onq*_*tam 1 c++ c-preprocessor
这是代码:
#define STR_CONCAT_IMPL(s1, s2) s1##s2
#define STR_CONCAT(s1, s2) STR_CONCAT_IMPL(s1, s2)
#define TOSTR_IMPL(x) #x
#define TOSTR(x) TOSTR_IMPL(x)
#define STR_CONCAT_TOSTR(s1, s2) TOSTR(STR_CONCAT(s1, s2))
int main() {
const char* a = STR_CONCAT_TOSTR(name, p); // works
const char* b = STR_CONCAT_TOSTR(name, =); // doesn't work
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要一个字符串像"name="从"name"在编译时(运行时并置不是一个选项),所以我试图用我的旧宏,但我得到这个错误:
error: pasting "name" and "=" does not give a valid preprocessing token
Run Code Online (Sandbox Code Playgroud)
但是当不使用=但是正常的角色时 - 它有效.
我怎样才能让它发挥作用?
需要C++ 98 GCC/MSVC解决方案.
我假设你需要一个宏,不能只使用编译器本身.
利用连接的相邻字符串文字:
#define STR_CONCAT_TOSTR(s1, s2) TOSTR(s1) TOSTR(s2)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,STR_CONCAT_TOSTR(name, =)扩展为"name" "="编译器变为"name=".
| 归档时间: |
|
| 查看次数: |
42 次 |
| 最近记录: |