序列化参数和消息的可变参数列表

时间:2015-11-22 02:39:27

标签: c++ macros

我正在尝试序列化任意参数列表。例如

const X* const* const

应该导致int a = 5; int b = 10; std::cout << EH_SERIALIZE(a, b);

这就是我所拥有的并且有效:

a = 5, b = 10

问题在于

#define EH_STRINGIZE(arg) EH_STRINGIZE1(arg)
#define EH_STRINGIZE1(arg) EH_STRINGIZE2(arg)
#define EH_STRINGIZE2(arg) #arg

// Make a FOREACH macro
#define EHFE_1(WHAT, X) WHAT##_FINAL(X)
#define EHFE_2(WHAT, X, ...) WHAT(X) EHFE_1(WHAT, __VA_ARGS__)
#define EHFE_3(WHAT, X, ...) WHAT(X) EHFE_2(WHAT, __VA_ARGS__)
#define EHFE_4(WHAT, X, ...) WHAT(X) EHFE_3(WHAT, __VA_ARGS__)
#define EHFE_5(WHAT, X, ...) WHAT(X) EHFE_4(WHAT, __VA_ARGS__)
//... repeat as needed

#define EH_GET_MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME
#define EH_FOR_EACH(action, ...) \
    EH_GET_MACRO(__VA_ARGS__, EHFE_5, EHFE_4, EHFE_3, EHFE_2, EHFE_1)(action, __VA_ARGS__)

// Example
// Some actions
#define EH_HELPER_SERIALIZE_FINAL(X) EH_STRINGIZE(X) " = " << (X) << "\n"
#define EH_HELPER_SERIALIZE(X) EH_STRINGIZE(X) " = " << (X) << ", "
#define EH_SERIALIZE(...) EH_FOR_EACH(EH_HELPER_SERIALIZE, __VA_ARGS__)

结果为int a = 5; int b = 10; std::cout << EH_SERIALIZE("message", a, b); 我想要的结果是"message" = message, a = 5, b = 10

我需要做些什么来检测第一个参数是否是字符串文字并以某种方式改变行为。

0 个答案:

没有答案