A preprocessor strobe operator with literary string prefixes
So, I want to do the traditional string statement thing in a macro:
#define FOO(x) foo(#x, (x))
However, I need to use the string literal prefix: http://en.cppreference.com/w/cpp/language/string_literal
This is a problem, because if I need a UTF-32 string literal, I try to do this:
#define FOO(x) foo(U#x, (x))
But gcc 4.9.2 complains:
error: 'U' was not declared in this scope
Is there a way to force the compiler to treat U
as a prefix a string macro variable?
+3
source to share
1 answer
Yes, use concatenation :
#define CONCAT2(x, y) x ## y
#define CONCAT(x, y) CONCAT2(x, y)
#define STRINGIZE(x) #x
#define FOO(x) foo(CONCAT(U, STRINGIZE(x)), (x))
Additional indirection, besides being good, if you pass a macro to be evaluated first, is "necessary" because of N3936 ยง16.3.2 [cpp.stringize] / 2, which says:
The order of evaluation of the # and ## operators is not specified.
+5
source to share