【发布时间】:2021-12-26 07:27:15
【问题描述】:
我正在尝试实现以下目标:
#define def_name(delim, ...) ??? // how will this variadic macro concatenate its parameters to define a new variable?
// Calling `def_name` as follows should define a new variable.
def_name("_", "abc", "def", "ghi");
// The following code should be generated after invoking the above macro.
inline constexpr char const abc_def_ghi_name[]{"abc_def_ghi"};
// Invoking the macro as:
def_name("", "abc", "def", "ghi");
// should produce the following code:
inline constexpr char const abcdefghi_name[]{"abcdefghi"};
def_name 宏应该是什么来支持上述用例?另外,是否可以在编译时使用 C++ 模板/constexpr 实现类似的功能?
【问题讨论】:
-
搜索 stringify.
-
你可以改成
def_name(_, abc, def, ghi)吗? -
@NateEldredge 是的,这也没关系。但是如何提供一个空的分隔符呢?
-
参数的数量真的必须是任意的,还是可以设置一个上限,比如 10 之类的?
-
@NateEldredge 理想情况下,任意数量的参数都很好,但预定的上限也可以。
标签: c++ c++17 string-concatenation variadic-macros