Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试编写一个宏来从常规字符串文字中创建用户定义的文字。以下两行应创建令牌"foobar"_literal:
"foobar"_literal
#define AS_LITERAL(TEXT) TEXT ## _literal AS_LITERAL("foobar");
但是GCC-4.7的预处理器报如下错误:
error: pasting ""foobar"" and "_literal" does not give a valid preprocessing token
如何正确地做到这一点?
它适用于适当定义的operator "" _literal(也使用 GCC 4.7):
operator "" _literal
#include <cstdlib> #define AS_LITERAL(TEXT) TEXT ## _literal constexpr int operator "" _literal(char const*, std::size_t) { return 0; } int main() { int x = AS_LITERAL("abc"); }