Differences in Macro ## concatenation operator between Visual-C++ and gcc

Florian Storck picture Florian Storck · Jul 30, 2009 · Viewed 7.9k times · Source

I'm having a macro like this ( not exactly, but function is quite equivalent):

#define STRUCTMEMBER(Member,Value) GlobalStructInstance. ## Member = Value
...
STRUCTMEMBER(Item,1);

This works perfectly in Visual C++, but gcc 3.4.5 (MingGW) yield the following error:

pasting "." and "Item" does not give a valid preprocessing token

This also happens when I use the "->" operator. I didn't found hints on concatenation, that the usage of these operators is forbidden.

Does anyone have an idea ?

Answer

Steve Jessop picture Steve Jessop · Jul 30, 2009

Maybe Visual C++ is pasting a couple of spaces together to make another space. Not that whitespaces are tokens, but it would allow your code to work.

object.member is not a token, it's three tokens, so you don't need token-pasting to implement the macro you describe. Just remove the '##' and it should work everywhere.

[Edit: just checked, and the result of using ## to form something that isn't a valid token is undefined. So GCC is allowed to reject it and MSVC is allowed to ignore it and perform no paste, as far as I can tell.]