我遇到以下问题。我有一些类将输入数组映射到输出数组。我想有float类型,以及阵列作为模板参数的长度,所以映射类是这样的:类型,积分常量和模板模板参数的变量模板
template <typename FloatType, std::size_t input, std::size_t output>
class Mapper
{};
template <typename FloatType, std::size_t input, std::size_t output>
class FirstMapper : public Mapper<FloatType, input, output>
{};
template <typename FloatType, std::size_t input, std::size_t output>
class SecondMapper : public Mapper<FloatType, input, output>
{};
到目前为止好。我的目标是编写一个堆叠这些Mapper类的不同实例的类。我希望能写出这样的代码:
StackedMapper<
double, // the FloatType, obviously
input_1, // the size of the first mapper's input array
FirstMapper, // the template template type of the first mapper
input_2, // the size of the first mapper's output and
// second mapper's input array
SecondMapper, // the template template type of the second mapper
input_3, // the size of the second mapper's output and
// third mapper's input array
FirstMapper, // the template template type of the third mapper
output // the size of the third mapper's output array
// ... any additional number of Mapper classes plus output sizes
> stacked_mapper;
内部,StackedMapper
类应该映射器实例存储在std::tuple
。我期望元组有以下类型:
std::tuple<
FirstMapper<double, input_1, input_2>,
SecondMapper<double, input_2, input_3>,
FirstMapper<double, input_3, output>
// ...
>;
如省略号所示,我想添加任意数量的Mapper类。正如您从注释中看到的那样,一个图层的输出大小等于下一个图层的输入大小。对于堆栈中的所有映射器,浮点类型只能定义一次。
有没有人有想法?我见过this问题,它解决了交替类型(积分常量和类型)问题,但它似乎不适用于模板模板参数,因为我总是遇到类似expected a type, got 'FirstMapper'
的错误。
有没有人有这个想法?
如果您对所有内容使用类型,而不是模板模板参数和非类型模板参数,则更方便。阅读@ Yakk元编程答案中的一个例子,例如http://stackoverflow.com/a/32056973/ – dyp
@dyp:感谢发布该链接,这是一个好的 –