[英]Preprocessor macros as parameters to other macros
The following C++ code compiles and works as the programmer intended on GCC (4.0.4) 以下C ++代码编译并作为GCC上的程序员工作(4.0.4)
#define FOO(x,y,z) ((x)*(y)*(z))
#define BAR(x) FOO(x,1)
#define BAZ 3,7
int main()
{
return BAR(BAZ); /* interpreted as return ((3)*(7)*(1)); */
}
However, the macros cause an error on Microsoft Visual C++ Express 2010: 但是,宏在Microsoft Visual C ++ Express 2010上导致错误:
main.cpp(7): warning C4003: not enough actual parameters for macro 'FOO'
main.cpp(7):警告C4003:宏'FOO'的实际参数不够
main.cpp(7): error C2059: syntax error : ')'main.cpp(7):错误C2059:语法错误:')'
The issue seems to be that the Microsoft compiler, while handling the BAR macro internally, does not expand the BAZ macro to parameters that could be used as two separate parameters to macro FOO. 问题似乎是Microsoft编译器在内部处理BAR宏时,不会将BAZ宏扩展为可用作宏FOO的两个独立参数的参数。
According to the standard, which compiler handles the situation correctly? 根据标准,哪个编译器正确处理了这种情况?
According to 16.3.4 of ISO/IEC 14882:2003 (C++ Stardard) macro expansion is performed as follows: 根据ISO / IEC 14882:2003(C ++ Stardard)的16.3.4,宏扩展如下进行:
The sequence of steps for the code you specified is: 您指定的代码的步骤顺序如下:
BAR(BAZ)
FOO(3,7,1)
((3)*(7)*(1))
So GCC is right, and VC is not. 所以海湾合作委员会是对的,VC不是。 But the error which VC complains about is that
FOO
has 3 arguments and BAR
specifies only 2 of them. 但VC抱怨的错误是
FOO
有3个参数而BAR
只指定了2个参数。 VC apparently tries to catch errors as soon as possible and goes a bit too far in it. VC显然试图尽快发现错误并且有点太过分了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.