【发布时间】:2013-12-27 07:41:56
【问题描述】:
是否可以写一个可变参数模板类
template<typename Functor, int... D>
struct Foo
{
void bar()
{
// ???
}
};
相当于
template<typename Functor, int D0>
struct Foo<Functor, D0>
{
void bar()
{
Functor f;
double d0[D0];
f(d0);
}
};
template<typename Functor, int D0, int D1>
struct Foo<Functor, D0, D1>
{
void bar()
{
Functor f;
double d0[D0];
double d1[D1];
f(d0, d1);
}
};
// And so on...
也就是说,传递给函子的参数数量等于模板参数的数量。参数应该在堆栈上分配。
【问题讨论】:
标签: c++ c++11 variadic-templates