c++ - 使用 boost 预处理实例化模板函数和类

标签 c++ c++11 boost macros

使用看似标准的 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)

这是可行的,但显然不够。

  1. 为什么这不能用于序列?

    #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)
    
  2. 我应该如何构建 template rtype func_name < {w,x,y,z} > {args,in,__VA_ARGS__} ?我尝试了很多不同的东西,但问题似乎是无法例如仅提取 w然后循环 __VA_ARGS__ ,然后继续。我一直在努力寻找 BOOST_PP_LIST_FOR_EACH_R 上类。这至少是正确的看法吗?

  3. 作为完整性检查,您不能在宏中定义宏,对吗?本着 spirit 的东西

    #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 似乎更容易实现此目的)如果这意味着什么。感谢您提供任何建议/资源。

最佳答案

您的问题似乎是您需要向BOOST_PP_(LIST|SEQ)_FOR_EACH 中的MACRO 传送多条数据,而只有一个“插槽”你可以使用。您似乎缺少的是您可以将这些部分分组,例如,一个元组,然后使用 BOOST_PP_TUPLE_ELEM 访问 MACRO 中的不同元素。像这样的东西可以工作:

//These are not required, just to help with readability
#define MACRO_GET_RETURN_TYPE(TUPLE)  BOOST_PP_TUPLE_ELEM(3,0,TUPLE)
#define MACRO_GET_FUNC_NAME(TUPLE)  BOOST_PP_TUPLE_ELEM(3,1,TUPLE)
#define MACRO_GET_ARGS_SEQ(TUPLE)  BOOST_PP_TUPLE_ELEM(3,2,TUPLE)


#define MACRO(_, DATA, ELEM) template MACRO_GET_RETURN_TYPE(DATA) MACRO_GET_FUNC_NAME(DATA) < ELEM > (BOOST_PP_SEQ_ENUM(MACRO_GET_ARGS_SEQ(DATA)));

// with boost seq
#define SEQ (x)(y)(z)(w)

#define INSTANTIATE_FUNC(rtype, func_name, ...) \
    BOOST_PP_SEQ_FOR_EACH(MACRO, (rtype,func_name,BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)), SEQ)

Live on Wandbox

PS:不,你不能在宏中定义宏,你在这里发布的代码确实适用于序列,你的要点中的代码不是因为使用了错误的标题。

关于c++ - 使用 boost 预处理实例化模板函数和类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612724/

相关文章:

c++ - C++ 中的 3 维数组

c++ - 调用了错误类型的显式构造函数

c++ - 无法加载共享对象 : undefined symbol

c++ - 将 hDC 转换为 4bpp 位图

c++ - 如何使用预签名的 url 和 Aws::FStream 将文件上传到 AWS S3?

c++ - QSql数据库 : QMYSQL driver not loaded on Mac OS

c++ - 没有为 C++ 类函数指针注册的 python 类

c++ - Protocol Buffer 问题,多个序列化为二进制文件

c++ - glm glx 旋转不起作用

c++ - 为什么boost的counting_iterator是常量?