http://gcc.gnu.org/onlinedocs/gcc/Typeof.html
typeof is a way to refer to the type of an expression, supported by gcc
e.g.
#define max(a, b)/
({typeof(a) _a = (a);/
typeof(b) )_b =(b);/
_a>_b?_a:_b;})
To declare y as an array of the same type as x:
typeof (x) y[4];
又如: #define pointer(T) typeof(T*)
e.g. to cast a member of a structure to the container structure
#define container_of(ptr, type, member) ({ /
const typeof(((type *)0)->member)) *__mptr = (ptr); /
(type *)( (char *)__mptr - offsetof(type, member)); })
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
本文介绍了 GCC 扩展关键字 typeof 的使用方法,通过示例展示了如何利用 typeof 获取表达式的类型,并应用于宏定义中实现灵活的类型操作,如类型匹配、指针定义及结构体内存偏移等。
986

被折叠的 条评论
为什么被折叠?



