在我们的遗留代码以及现代代码中,我们使用宏来执行代码生成等漂亮的解决方案。我们同时使用 #
和 ##
运算符。
我很好奇其他开发人员如何使用宏来做很酷的事情,如果他们使用宏的话。
最佳答案
在 C 中,通常定义宏来执行某些操作以获取逐字参数,同时定义能够透明地获取其地址的函数。
// could evaluate at compile time if __builtin_sin gets
// special treatment by the compiler
#define sin(x) __builtin_sin(x)
// parentheses avoid substitution by the macro
double (sin)(double arg) {
return sin(arg); // uses the macro
}
int main() {
// uses the macro
printf("%f\n", sin(3.14));
// uses the function
double (*x)(double) = &sin;
// uses the function
printf("%f\n", (sin)(3.14));
}
关于c++ - 我可以对宏使用哪些技巧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650461/