MistyD
MistyD

Reputation: 17253

Concatenate a macro parameter with text

I am currently working on porting some code from Visual Studio to Mingw GCC. The code builds and runs fine in visual studio but has some issues on Mingw GCC. One of the issue I am having is with Macro Expansion. I have simplified this example here.

#define DECLARE_LEGACY_TYPES(ClassType) \
        typedef LG_Wrapper::##ClassType##<LG_Wrapper::TA>               ClassType; \
        typedef LG_Wrapper::##ClassType##<LG_Wrapper::TB>           ClassType##TPR; \
        typedef LG_Wrapper::##ClassType##<LG_Wrapper::TC>   ClassType##DBN; \

Now when I attempt to build this I get the error(s)

 error: pasting "::" and "GraphicsDevice" does not give a valid preprocessing token
   typedef LG_Wrapper::##ClassType##<LG_Wrapper::TA>    ClassType; \
                     ^
 note: in expansion of macro 'DECLARE_LEGACY_TYPES'
 DECLARE_LEGACY_TYPES(GraphicsDevice);
 ^
 error: pasting "GraphicsDevice" and "<" does not give a valid preprocessing token
 DECLARE_LEGACY_TYPES(GraphicsDevice);

Any suggestions on why I get this issue in mingw GCC and not in Visual Studio and how would i resolve this issue ?

Upvotes: 0

Views: 334

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39651

Either remove the first two ## from each line of the DECLARE_LEGACY_TYPES macro or create a new version without them. They're completely unnecessary, and the error GCC is giving is allowed by the Standard C++, if not actually required. The result of using the ## preprocessing operator must be a valid preprocessing token and ::GraphicsDevice isn't a valid preprocessing token.

You want something like this:

#define CONFORMING_DECLARE_LEGACY_TYPES(ClassType)                    \
        typedef LG_Wrapper::ClassType<LG_Wrapper::TA> ClassType;      \
        typedef LG_Wrapper::ClassType<LG_Wrapper::TB> ClassType##TPR; \
        typedef LG_Wrapper::ClassType<LG_Wrapper::TC> ClassType##DBN; 

Upvotes: 1

Related Questions