How to compose stringification with user defined literal (UDL) in Macro?

自作多情 提交于 2020-01-25 06:52:29

问题


How to use literal suffix for identifier transformed into literal string in MACRO by #identifier?

struct SomeType;
SomeType operator "" _udl(const char* self);

#define STRINGIFY_AS_UDL(id) /* #id _udl doesn't work */ /* How to have "id"_udl */ 

STRINGIFY_AS_UDL(foo) // -> "foo"_udl
STRINGIFY_AS_UDL(bar) // -> "bar"_udl
STRINGIFY_AS_UDL(42)  // -> "42"_udl

回答1:


UDL operators are also "regular" functions, so you can call them instead:

#define STRINGIFY_AS_UDL(id) operator ""_udl(#id)

but you can use the token-pasting operator ##:

#define STRINGIFY_AS_UDL(id) #id ## _udl

or concatenation of adjacent strings:

#define STRINGIFY_AS_UDL(id) #id ""_udl

Note that any of the concatenation method would be required for template UDL for string (extension of gcc/clang):

// gcc/clang extension
template<typename Char, Char... Cs>
/*constexpr*/ SomeType operator"" _udl();

// Usage
// "some text"_udl


来源:https://stackoverflow.com/questions/58868806/how-to-compose-stringification-with-user-defined-literal-udl-in-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!