The following C++ code compiles and works as the programmer intended on 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:
main.cpp(7): warning C4003: not enough actual parameters for macro 'FOO'
main.cpp(7): error C2059: syntax error : ')'
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.
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:
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. But the error which VC complains about is that FOO
has 3 arguments and BAR
specifies only 2 of them. VC apparently tries to catch errors as soon as possible and goes a bit too far in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With