在编译时使用宏和标记串联在宽字符串和窄字符串之间切换是很常见的.
#define _T(x) L##x
const wchar_t *wide1 = _T("hello");
const wchar_t *wide2 = L"hello";
Run Code Online (Sandbox Code Playgroud)
在C++ 11中,用原始字符串编写类似的东西应该是有效的:
#define RAW(x) R##x
const char *raw1 = RAW("(Hello)");
const char *raw2 = R"(Hello)";
Run Code Online (Sandbox Code Playgroud)
由于宏扩展和令牌连接在转义序列替换之前发生,因此这应该防止在引用的字符串中替换转义序列.
但这如何适用于三元组?是否通过连接正常字符串形成的原始字符串仍然需要恢复其三字符替换?
const char *trigraph = RAW("(??=)"); // Is this "#" or "??="?
Run Code Online (Sandbox Code Playgroud)