C++ - macro parameter substitution for string literals?

Started by
5 comments, last by DigitalDelusion 19 years, 9 months ago
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.
Advertisement
Yes, use a # prefix:
#define FOO(BAR) do_stuff(#BAR)
// FOO(baz) --> do_stuff("baz")
#define FOO( BAR_ ) do_stuff ( #BAR_ )
what you wanted to say was:
#define FOO(x) do_stuff( #x)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
#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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
That was quick. Thanks.
Hehe four replies on the same minute...
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement