【发布时间】:2017-06-18 07:34:11
【问题描述】:
使用看似标准的 w、x、y、z 演示,假设我有以下宏试图转换为“可迭代”预处理器宏
#define INSTANTIATE_FUNC(rtype, func_name, ...) \
template rtype func_name< w > (__VA_ARGS__); \
template rtype func_name< x > (__VA_ARGS__); \
template rtype func_name< y > (__VA_ARGS__); \
template rtype func_name< z > (__VA_ARGS__);
为了完整起见,假设我们正在尝试实例化以下内容
struct w { static constexpr int data = 0; };
struct x { static constexpr int data = 1; };
struct y { static constexpr int data = 2; };
struct z { static constexpr int data = 3; };
template <class Data>
void printData(const std::string &prefix) {
std::cout << prefix << Data::data << std::endl;
}
INSTANTIATE_FUNC(void, printData, const std::string &prefix)
为了方便起见,我创建了一个minimal gist with a build system,这样如果您有兴趣尝试,就不必重新创建所有内容:)
我不知道如何处理这个问题。唯一有功能(但没用)的刺
#include <boost/preprocessor/list/for_each.hpp>
#define LIST (w, (x, (y, (z, BOOST_PP_NIL))))
#define MACRO(r, data, elem) template void data < elem > (const std::string &prefix);
#define INSTANTIATE_FUNC(rtype, func_name, ...) \
BOOST_PP_LIST_FOR_EACH(MACRO, func_name, LIST)
这是可行的,但显然还不够。
-
为什么这也不适用于序列?#include <boost/preprocessor/seq/for_each.hpp> // this does work, my code included the wrong header // on what I was testing with (seq/for_each_i.hpp) #define SEQ (x)(y)(z)(w) #define INSTANTIATE_FUNC(rtype, func_name, ...) \ BOOST_PP_SEQ_FOR_EACH(MACRO, func_name, SEQ) 我应该如何构建
template rtype func_name < {w,x,y,z} > {args,in,__VA_ARGS__}?我尝试了很多不同的东西,但问题似乎无法解决,例如只提取w,然后循环遍历__VA_ARGS__,然后继续。我一直在努力让BOOST_PP_LIST_FOR_EACH_R工作。这至少是正确的吗?-
作为健全性检查,您不能在宏中定义宏,对吧?本着精神的东西
#define INSTANTIATE_FUNC(rtype, func_name, ...) \ #define MACRO_##func_name(r, data, elem) data < elem > (__VA_ARGS__); \ BOOST_PP_LIST_FOR_EACH(MACRO_##func_name, func_name, LIST)
我最终将朝着启用LIST/SEQ 的可选扩展的目标努力(SEQ 似乎更容易实现这一点)如果这意味着什么。感谢您提供任何建议/资源。
【问题讨论】: