Sign in to follow this  
Painless

C++ - macro parameter substitution for string literals?

Recommended Posts

Painless    126
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
Guest Anonymous Poster   
Guest Anonymous Poster
#define FOO( BAR_ ) do_stuff ( #BAR_ )

Share this post


Link to post
Share on other sites
Fruny    1658
#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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this