Reputation: 2326
The approach to concatenate in C/C++ in a preprocessor macro is to use ##. The approach to stringify is to use #. I'm trying to concat AND stringify. This is generating a warning from g++ (3.3.2)
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y) // concat
#define TOKENPASTE3(x, y) TOKENPASTE(#x, #y) // concat-stringify (warnings)
const char* s = TOKENPASTE3(Hi, There)
It is not acceptable to get the warning
"test_utils/test_registration.h:34:38: warning: pasting ""Hi"" and ""There"" does not give a valid preprocessing token"
although (using the -E option) I see that it generates:
const char* s = "Hi""There";
Which looks right to me.
Any help will be appreciated.
Upvotes: 6
Views: 11684
Reputation:
The preprocessor already concatenates adjacent string literals. So your macro is unnecessary. Example:
#define TOKENPASTE3(x, y) #x #y
const char* s = TOKENPASTE3(Hi, There);
becomes "Hi" "There"
. However, if you wanted to stick with your approach, you need to use an extra level of indirection to macro expand your new token:
#define STRINGIFY(x) #x
#define TOKENPASTE(x, y) STRINGIFY(x ## y)
becomes "HiThere"
.
Upvotes: 9