【问题标题】:template instantiation with boost: pass extra arguments带有 boost 的模板实例化:传递额外的参数
【发布时间】:2017-05-19 16:50:31
【问题描述】:

说我有表单的实例

template void MyClass<2,typeA>::some_method() const;

用于模板参数的各种组合。

我正在尝试定义一个带有 boost 的宏,它需要一个前缀 (void) 和一个后缀 some_method() const,以便我可以将它重用于显式实例化。

使用硬编码的返回类型和函数名称,可以按照以下方式完成(尚未测试过):

#define DIM (2)(3)
#define DEG (typeA)(typeB)
#define INSTANTIATEONE(_, targs) void MyClass<BOOST_PP_SEQ_ENUM(targs)>::some_method() const;
#define INSTANTIATEALL() BOOST_PP_SEQ_FOR_EACH_PRODUCT(INSTANTIATEONE, (DIM)(DEG));

有没有办法扩展宏以将void 和some_method() const 作为参数?我是增强预处理器的新手,不知道该怎么做。

【问题讨论】:

    标签: c++ templates boost instantiation


    【解决方案1】:

    我最终使用了

    # include <boost/preprocessor/facilities/empty.hpp>
    # include <boost/preprocessor/list/at.hpp>
    # include <boost/preprocessor/list/for_each_product.hpp>
    # include <boost/preprocessor/tuple/elem.hpp>
    # include <boost/preprocessor/tuple/to_list.hpp>
    
    #define DIM BOOST_PP_TUPLE_TO_LIST(2,(2,3))
    #define DEG BOOST_PP_TUPLE_TO_LIST(1,(typeA))
    
    #define INSTANTIATE(R, L) \
        template void MyClass<BOOST_PP_TUPLE_ELEM(2, 0, L), \
                              BOOST_PP_TUPLE_ELEM(2, 1, L)>::some_method() const;                            
    BOOST_PP_LIST_FOR_EACH_PRODUCT(INSTANTIATE, 2, (DIM, DEG))
    

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2016-05-03
      相关资源
      最近更新 更多