【发布时间】:2019-08-25 20:37:52
【问题描述】:
一般的想法是有一个通用类来表示n维空间中的一个点(“类Point”)。只要它们的类型相同,它就应该采用任意数量的参数。 我使用可变参数模板参数包做到了这一点,它似乎工作正常。
现在我想要一个简单的别名,比如“Point2i”,它一次只接受整数参数(简单,只需指定类型 T)和两个参数(提示二维空间)(这是我失败的地方到目前为止)。
/**
* @brief Point in n-dimensional space.
*/
template <typename T = int> class Point {
public:
/**
* @brief Default constructor for an empty polygon.
* Points have to be added manually via @ref add_point.
*/
template <typename... Ts>
Point(Ts... coords) {
m_coordinates = { std::forward<Ts>(coords)... };
}
/**
* @brief Dimensions of the point coordinate space.
* @return Number of dimensions.
*/
size_t dims() const {
return m_coordinates.size();
}
/**
* @brief Array subscript operator.
* @param dim The dimension you want.
* @return The coordinate in the specified dimension.
*/
T& operator[] (const size_t &dim) {
return m_coordinates[dim];
}
private:
/// Coordinates of the point in n-dimensional space, where n = vector size.
std::vector<T> m_coordinates;
};
template <typename... Ts, typename = typename std::enable_if<sizeof...(Ts) == 2>::type>
using Point2i = Point<int>(Ts...);
问题出在最后两行:“Point2i”的东西不起作用。我从 GCC 9 得到的错误是:“point.h:52:23: error: template parameter pack must be the last template parameter”。
第 52 行是带有“模板”的行
知道如何按照我想要的方式进行这项工作吗?我想对于有 C++ 模板元编程经验的人来说这很容易。
【问题讨论】:
-
您不能为对构造函数的特定调用创建类型别名
-
一般情况下,您不应该将类与动态分配的数组一起用于二维点。这将对性能造成巨大影响。您应该通过
T val[N];或std::array<T,N> val;编写一个带有静态分配数据的单独类。 -
另一个问题,对
Point类使用模板参数包构造函数不是一个好主意。你最好通过std::initializer_list<T>写一个简单的非模板构造函数。
标签: c++ templates variadic-templates