You can use the built-in function __builtin_constant_p to determine if a value is known to be constant at compile time and hence that GCC can perform constant-folding on expressions involving that value. The argument of the function is the value to test. The function returns the integer ...
Oracle Solaris Studio 12.2:C 用户指南 2.14.4__builtin_constant_p() __builtin_constant_p是编译器内置函数。它接受一个数值参数,如果已知参数是一个编译时常量,则返回 1。返回值 0 意味着编译器无法确定参数是否是编译时常量。此内置函数的典型用法是在宏中用于手动编译时优化。
GCC内建函数__builtin_constant_p,__builtin_constant_p是GCC的内建函数,可以用该函数判断传入的参数是否为编译时常量。如果是编译时常量则该函数返回1,否则返回0。如果该函数返回0,并不代表参数不是常数,而仅仅是在“-O”优化参数下,GCC编译器无法证明其是否为常量。
例如,int len = sizeof(struct ...);紧接着的__builtin_constant_p(len)返回值就是1,尽管len是变量。
可以认为这个函数的求值在编译时就完成了。从而外面的条件跳转语句的两个分支,只有其中一个会出现在目标代码中。 准确的定义应该是:如果x的值在编译时能确定,那么该函数返回值为1. 例如,int len = sizeof(struct ...);紧接着的__builtin_constant_p(len)返回值就是1,尽管len是变量。
int __builtin_constant_p (exp); You can use the built-in function __builtin_constant_p to determine if a value is known to be constant at compile-time
__builtin_constant_p is a compiler builtin function. It takes a single numeric argument and returns 1 if the argument is a compile-time constant. A return value of 0 means that the compiler can not determine whether the argument is a compile-time constant. A typical use of this built-...
正如评论中所指出的,即使是GCC和Clang在这里的行为也是不同的,所以结论是编译器内置的定义是松散的,...
handling of the use case for this use of __builtin_constant_p (in particular, detecting constant insn conditions when building GCC), such expressions as (0 && foo()) should be accepted as constant by __builtin_constant_p (0 may have been a macro expansion of TARGET_64BIT or ...
基本上,没有人会将大段的C语言代码全部塞入 main() 函数,更好的做法是按照复用率高,耦合性低的...