在Linux内核中,结构化类型的定义如下:
typedef struct _TAG_ { ... };
然后用于这样的例程:
struct _TAG_ structured_entity;
struct _TAG_ *pointer_to_structured_entity;
void function(struct _TAG_ *arg, ...);
为什么不这样:
typedef struct _TAG_ { ... } _typename_;
然后:
_typename_ structured_entity;
_typename_ *pointer_to_structured_entity;
void function(_typename_ *arg, ...);
这样做的技术必要性是什么?或者这只是传统/风格/魔术?
答案 0 :(得分:2)
在kernel.org和内核附带的Documentation目录中的Linux kernel coding style - 不鼓励对结构使用typedef:
Chapter 5: Typedefs Please don't use things like "vps_t". It's a _mistake_ to use typedef for structures and pointers. When you see a vps_t a; in the source, what does it mean? In contrast, if it says struct virtual_container *a; you can actually tell what "a" is.
该文档继续列出作者认为typedef很有用的情况 - 例如只能使用访问器函数访问的不透明对象 - 以下结尾:
In general, a pointer, or a struct that has elements that can reasonably be directly accessed should _never_ be a typedef.