使用提升的模板实例化:传递额外的参数

template instantiation with boost: pass extra arguments

本文关键字:参数 实例化      更新时间:2023-10-16

>说我有表单的实例化

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));

有没有办法扩展宏以voidsome_method() const作为参数?我是提升预处理器的新手,不知道该怎么做。

我最终使用

# 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))