S
Sabyasachi Basu
While trying to port some stuff from Unix to Windows, I encountered a
strange behaviour of function macros with empty arguments. Here is a small
snippet which illustrates the problem:
#include <iostream>
#include <string>
using namespace std;
#define B(X, Y) Y
int main()
{
string mystr (B(, "hello world"));
cout << mystr << std::endl;
return 0;
}
This correctly prints 'hello world' when compiled with g++. However, this
program fails to compile on VC++. On going through the preprocessor output
in VC++, I found that the macro call line is:
string mystr ();
This expansion is incorrect.
g++ does the correct expansion (as documented:
http://gcc.gnu.org/onlinedocs/gcc-3.0.1/cpp_3.html#SEC15 )
and the output for the same statement for the g++ preprocessor is:
string mystr ("hello world");
On fiddling around a little more, I realised that the VC++ preprocessor
shifts to the left the non-empty arguments passed, if any. For instance, on
changing the macro definition to:
#define B(X, Y) X X
the statement string mystr (B(, "hello world")); got incorrectly
expanded on VC++ to
string mystr ("hello world" "hello world");
while g++ correctly expanded it to
string mystr ( );
I am using VC++ 6.0
-- Saby
strange behaviour of function macros with empty arguments. Here is a small
snippet which illustrates the problem:
#include <iostream>
#include <string>
using namespace std;
#define B(X, Y) Y
int main()
{
string mystr (B(, "hello world"));
cout << mystr << std::endl;
return 0;
}
This correctly prints 'hello world' when compiled with g++. However, this
program fails to compile on VC++. On going through the preprocessor output
in VC++, I found that the macro call line is:
string mystr ();
This expansion is incorrect.
g++ does the correct expansion (as documented:
http://gcc.gnu.org/onlinedocs/gcc-3.0.1/cpp_3.html#SEC15 )
and the output for the same statement for the g++ preprocessor is:
string mystr ("hello world");
On fiddling around a little more, I realised that the VC++ preprocessor
shifts to the left the non-empty arguments passed, if any. For instance, on
changing the macro definition to:
#define B(X, Y) X X
the statement string mystr (B(, "hello world")); got incorrectly
expanded on VC++ to
string mystr ("hello world" "hello world");
while g++ correctly expanded it to
string mystr ( );
I am using VC++ 6.0
-- Saby