• Advertisement
Sign in to follow this  

C++ - macro parameter substitution for string literals?

This topic is 4977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Search didn't turn up much. Is there a way to use a parameter of a macro in a string literal? For example: #define FOO( BAR_ ) do_stuff( "BAR_" ); (so FOO( baz ) becomes do_stuff( "baz" );) Simply writing it like that doesn't seem to work - the macro parameter is ignored inside the string. Edit: code tags don't work the old way.. Edit n+1: whatever, I can't get any of them to work.

Share this post


Link to post
Share on other sites
Advertisement
Yes, use a # prefix:
#define FOO(BAR) do_stuff(#BAR)
// FOO(baz) --> do_stuff("baz")

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
#define FOO( BAR_ ) do_stuff ( #BAR_ )

Share this post


Link to post
Share on other sites
#define STR2(X) #X
#define STR(X) STR2(X)

#define FOO(BAR) do_stuff(STR(BAR))


Will work even if BAR is itself a macro.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement