模板方法,其参数数量为实例化固定,但因模板参数而异
Template method with number of arguments fixed for instantiation but variable by template parameter
我想为模板类定义一个带有整型模板形参的函数,以便函数实参的数量取决于模板形参。下面是一个例子:
template< class Coord, int dim >
class Point {
Coord mCoords[ dim ];
public:
void Set( /* I want exactly dim Coord arguments here. */ );
};
我想编译以下代码:
Point<double,2> pt2d;
pt2d.Set( 25, 32 );
Point<double,3> pt3d;
pt3d.Set( 25, 32, 100 );
和下面的代码失败:
Point<double,2> pt2d;
pt2d.Set( 25, 32, 100 ); // Too many arguments
Point<double,3> pt3d;
pt3d.Set( 25, 32 ); // Too few arguments
现在,我可以在较小的维度上手动专门化Point
以具有不相关的Set
函数,但我发现基本上重复相同代码的做法是un- c++ -ish。此外,我不需要对int模板形参的每个可能值都进行专门化。
是否有可能实现Point<Coord,dim>::Set()
函数,该函数将完全采用Coord
类型的dim
参数,而无需为dim
的每个值编写专门化代码?
您可以使用Boost的技巧。getNth
:
template <typename Coord, int dim, typename = std::make_index_sequence<dim>>
struct Point;
template <typename Coord, int dim, size_t... Ignore>
struct Point<Coord, dim, std::index_sequence<Ignore...>>
{
void Set(decltype(Ignore, Coord{})... args)
{
// ...
}
};
一个较长的版本,隐藏了Ignore
的丑陋(并适用于非默认可构造的Coord
s…)将添加一些元编程样板:
template <typename... > struct typelist { };
template <int N, typename T, typename = std::make_index_sequence<N>>
struct repeat;
template <int N, typename T>
using repeat_t = typename repeat<N, T>::type;
template <int N, typename T, size_t... Idx>
struct repeat<N, T, std::index_sequence<Idx...>>
{
template <size_t >
struct makeT { using type = T; };
using type = typelist<typename makeT<Idx>::type...>;
};
然后专门研究repeat_t
。并将其隐藏在一个命名空间中,这样用户就不会把它弄乱:
namespace details {
template <typename Coord, int dim, typename = repeat_t<dim, Coord>>
struct Point;
template <typename Coord, int dim, typename... dimCoords>
struct Point<Coord, dim, typelist<dimCoords...>>
{
void Set(dimCoords... args)
{
}
};
}
template <typename Coord, int dim>
using Point = details::Point<Coord, dim>;
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如果模板参数是另一个模板的实例化,则键入特征测试
- 如何在模板函数中实例化其长度使用模板参数的数组
- 函数在可变参数模板的实例化期间不可见
- 实例化模板时,我是否必须显式显示参数包中的类型?
- 从模板参数包实例化的访问类实现
- C++ 可变参数模板实例化深度超过最大值 900
- 与参数匹配的友元模板函数实例化
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 在编译时检查未实例化的类模板是否继承自其第一个模板参数
- 如何将包装作为模板参数实例化专用模板类
- 如何在 c++ 中使用枚举参数实例化对象
- 使用NULL参数实例化模板类
- 使用模板参数实例化模板类
- 使用bool非类型参数实例化类模板时出错
- 从类别上防止或阻止使用特定参数实例化对象
- 如何用不同的参数实例化类
- 模板是否为相同类型的参数实例化多次
- C++:根据一个参数实例化一个模板化的类
- 用Constructor参数实例化类对象和不带参数C++的*运算符之间的区别