从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。
每周更新
欢迎投稿,推荐或自荐文章/软件/资源等,请提交 issue
介绍了gcc的架构以及处理流程,值得一看,拓展眼界
介绍了tuple中的空基类优化
Practical Design Patterns: Opaque Pointers and Objects in C
看代码
Ringbuffer.h
#pragma once
#include <stdint.h>
#include <stdbool.h>
// Opaque pointer type to represent a ringbuffer instance
// 作者管这个叫opaque pointer
typedef struct ringbuffer_instance_t* ringbuffer_t;
// Functions that operate on an instance
ringbuffer_t ringbuffer_create(uint32_t capacity);
uint32_t ringbuffer_capacity(ringbuffer_t instance);
bool ringbuffer_enqueue(ringbuffer_t instance, uint8_t item);
bool ringbuffer_dequeue(ringbuffer_t instance, uint8_t* item);
void ringbuffer_destroy(ringbuffer_t instance);
ringbuffer.c
#include "ringbuffer.h"
#include <stdlib.h>
// Private struct, only accessible from within this file
struct ringbuffer_instance_t {
volatile uint32_t wr_pos;
volatile uint32_t rd_pos;
uint8_t* data;
uint32_t capacity;
};
ringbuffer_t ringbuffer_create(uint32_t capacity) {
ringbuffer_t inst = calloc(1, sizeof(struct ringbuffer_instance_t));
inst->data = calloc(capacity, sizeof(uint8_t));
inst->capacity = capacity;
inst->wr_pos = 0;
inst->rd_pos = 0;
return inst;
}
uint32_t ringbuffer_capacity(ringbuffer_t instance) {
return instance->capacity;
}
bool ringbuffer_enqueue(ringbuffer_t instance, uint8_t item) {
// implementation omitted
return true;
}
bool ringbuffer_dequeue(ringbuffer_t instance, uint8_t* item) {
// implementation omitted
return true;
}
void ringbuffer_destroy(ringbuffer_t instance) {
if (instance) {
if (instance->data) {
free(instance->data);
}
free(instance);
}
}
作者管这个ringbuffer_t叫opaque pointer实际上就是一层不透明封装,当handle用。这个不是什么新的技巧了
暮无井见铃06-02 评论补充
Opaque pointer 就是 pImpl 的另一种习惯说法……其实很古老了,例如 C 标准都是允许 FILE 在头文件中仅声明不定义的。
Ambiguity in template parameters
讨论了一下模版NTTP和其他类型和concept一锅烩等场景,看个乐
Porting Intel Intrinsics to Arm Neon Intrinsics
手把手教你写sse移植代码(从x86到arm)
std::variant支持继承,再实现operator()更方便std::visit 在线演示godbolt
struct value : std::variant<int, double> {
using variant::variant;
constexpr auto operator()(const auto& value) {
std::clog << value << '\n';
}
};
int main(){
std::visit(value{}, std::variant<int, double>(42)); // prints 42
std::visit(value{}, std::variant<int, double>(99.)); // prints 99
}
How to build high DPI aware native Windows desktop applications
手把手教你写c++感应dpi的应用所需要的代码
A general state rollback technique for C++
介绍了一种管理状态的技巧,snapshot(数据库中常见)
C++20: Building a Thread-Pool With Coroutines
手把手教你写基于协程的线程池
代码
讲的代码设计重构之类的。没有仔细看