Tag: generics集合

具有宏的类型安全的通用容器

我正在尝试使用宏在C中创建一个类型安全的通用链表。 它应该与模板在C ++中的工作方式类似。 例如, LIST(int) *list = LIST_CREATE(int); 我的第一次尝试是#define LIST(TYPE) (我上面使用的宏)来定义struct _List_##TYPE {…} 。 但是,这不起作用,因为每次我声明一个新列表时都会重新定义结构。 我通过这样做解决了这个问题: /* You would first have to use this macro, which will define the `struct _List_##TYPE`… */ DEFINE_LIST(int); int main(void) { /* … And this macro would just be an alias for the struct, it wouldn’t actually define it. */ […]