linux - strange macro in c, using multiple statements -
this question has answer here:
i found strange syntax while reading linux source code. container_of macro looks like
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
what confused me syntax ({statement1; statement2;})
i tried simple code like
int = {1;2;};
i compiled gcc. after running, 'a' seemed 2. couldn't compiled microsoft vc++. syntax expanded feature of gcc? if so, how can same effect without gcc expansion, define multiple statements , return value using macro?
the ({
...})
syntax gcc extension, called statement expressions.
the typeof gcc extension.
both extensions available in other compilers, llvm/clang (or tinycc).
the linux kernel uses them quite often.
it quite difficult avoid them. if wanted to, might consider (and non trivial task), conversion gimple low-level, non-portable , unreadable c. might use melt (part of job done in file melt/xtramelt-c-generator.melt
j.salvucci).
Comments
Post a Comment