There was a discussion in the Linux kernel mailing list regarding a macro that tests whether its argument is an integer constant expression and is an integer constant expression itself.
One particularly clever approach that does not use builtins, proposed by Martin Uecker (taking inspiration from glibc's tgmath.h), is:
#define ICE_P(x) (sizeof(int) == sizeof(*(1 ? ((void*)((x) * 0l)) : (int*)1)))
This macro expands into an integer constant expression of value 1
if the argument is an integer constant expression, 0
otherwise. However, it relies on sizeof(void)
to be allowed (and different than sizeof(int)
), which is a GNU C extension.
Is it possible to write such a macro without builtins and without relying on language extensions? If yes, does it evaluate its argument?
For an explanation of the macro shown above, see instead: Linux Kernel's __is_constexpr Macro
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…