<分区>
我正在研究 Linux 的 container_of
函数,我发现 typeof 的括号的使用真的很困惑。
这是container_of
函数的代码:
#define container_of(ptr, type, member) ({ \
const typeof( ((type*)0)->member) * __mptr =(ptr);\
(type*)( (char*)__mptr - offsetof(type,member) );})
在第二行,我们有
const typeof( ((type*)0)->member) * __mptr =(ptr);
我不明白为什么我们在右边使用括号。
我在网上搜索了一下,发现了一些类似的用法。例如,来自 https://gcc.gnu.org/onlinedocs/gcc/Typeof.html , 我找到了一个函数 max(a,b)
#define max(a,b)
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; })
在这种情况下,我们确实有类似的用法 typeof (a) _a = (a);
为什么要在右边加括号?能不能把右边的括号去掉,变成typeof (a) _a = a;
?
提前致谢!