2

I have been writing C++ for a while but have very little macro experience. I have read some of the other questions on this topic but I just can't quite translate them to my problem.

I want to define a macro such that coding ENUM_PRAGMA(foo) produces _Pragma("enum(foo)") which I intend to have the effect of #pragma enum(foo)

(The compiler supports _Pragma("string").)

I have tried multiple variations of

#define ENUM_PRAGMA(siz)  \
_Pragma( "enum(" #siz ")" ) 

but can't get any of them to work.

Based on How do I implement a macro that creates a quoted string for _Pragma? I tried

#define HELPER1(x) enum( x )  
#define HELPER2(y) HELPER1(#y) 
#define ENUM_PRAGMA(siz) _Pragma(HELPER2(siz)) 

but I'm still not quite there. (Error is string literal was expected but enum was found so I guess my HELPER2 is not quoting the string.

Can anyone please humor me on this? Thanks much.

Community
  • 1
  • 1

1 Answers1

3

Okay, I got it.

I defined a general purpose macro STRINGIFY:

#define STRINGIFY(str) #str

Now the real macro comes down simply to

#define ENUM_PRAGMA(siz) _Pragma(STRINGIFY(enum(siz)))

Thanks for your patience.