如何在Macro中用用户定义文字(UDL)组成字符串化? [英] How to compose stringification with user defined literal (UDL) in Macro?

查看:113
本文介绍了如何在Macro中用用户定义文字(UDL)组成字符串化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用字面后缀将标识符由#identifier转换为MACRO中的字面量字符串?

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

推荐答案

UDL运算符也是常规"函数,因此您可以改为调用它们:

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

或相邻字符串的串联:

#define STRINGIFY_AS_UDL(id) #id ""_udl

请注意,字符串的模板UDL(gcc/clang的扩展名)将需要任何串联方法:

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

这篇关于如何在Macro中用用户定义文字(UDL)组成字符串化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆