带字符串文字前缀的预处理器字符串化运算符 [英] Preprocessor Stringizing Operator with String Literal Prefixes
问题描述
所以我想做传统的事情,与宏中的字符串化运算符有关:
So I want to do the traditional thing to do with the stringizing operator in a macro:
#define FOO(x) foo(#x, (x))
但是我需要使用字符串文字前缀: http://en.cppreference. com/w/cpp/language/string_literal
这是一个问题,因为如果我需要UTF-32字符串文字,我会尝试这样做:
However I need to use a string literal prefix: http://en.cppreference.com/w/cpp/language/string_literal
Which is a problem because if I need a UTF-32 string literal I try to do this:
#define FOO(x) foo(U#x, (x))
但是gcc 4.9.2抱怨:
But gcc 4.9.2 complains:
错误:未在此范围内声明"U"
error: 'U' was not declared in this scope
有没有办法使编译器将U
当作字符串化宏变量的前缀?
Is there a way to make the compiler treat the U
as a prefix to the stringized macro variable?
推荐答案
是的,使用串联:
#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))
由于N3936§16.3.2[cpp.stringize]/2,它表示:需要"额外的间接寻址(如果您传递应该首先进行评估的宏是好的),
The extra indirection, besides being good if you pass a macro that should be evaluated first, is "needed" because of N3936 §16.3.2 [cpp.stringize]/2, which says:
#和##运算符的求值顺序未指定.
The order of evaluation of # and ## operators is unspecified.
这篇关于带字符串文字前缀的预处理器字符串化运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!