使用模板化结构作为构造函数参数
Using templated struct as constructor argument
我有一个模板class Point 将存储x和y坐标,两者的数据类型可以是任何算术类型。
template <typename T, typename U>
struct Point {
T x;
U y;
};
Line class 将两个 Points 作为构造函数参数来查找直线方程。
class Line {
public:
Line(Point p1, Point p2) {}
};
如何设计 Line class 使其可以接受不同数据类型的 Point 对象?
我设计了类似下面的东西,我该如何改进它?
template <class T, class U,
class = typename std::enable_if<std::is_arithmetic<T>::value &&
std::is_arithmetic<U>::value>::type>
struct Point
{
T x;
U y;
typedef T x_type;
typedef U y_type;
};
template <typename T1,
typename T2,
typename P1 = typename T1::x_type,
typename P2 = typename T1::y_type,
typename P3 = typename T2::x_type,
typename P4 = typename T2::y_type>
class Line {
public:
Line(Point<P1, P2> p1, Point<P3, P4> p2) {}
};
我打算使用的方式是:
Point<int, long> p1 (2, 5);
Point<short, double> p2 (77, 33.5);
Line<Point<int, long>, Point<short, double>> line_(p1, p2);
OR
Line<Point<int, short>, Point<float, unsigned>> eq({1, 1}, {2.3f, 2});
改进或缩短代码的方法有哪些。
模板只是模板,不是类型。除非您编写一个函数模板,否则您不能拥有一个将模板作为参数的函数,或者如果您的 Line
是您已经拥有的 class 模板,但它可以写得更简单一些:
template <typename T,typename U>
class Line {
public:
Line(Point<T,U> p1,Point<T,U> p2) {}
};
或者对点的类型进行参数化 Line
:
template <typename P>
class Line {
public:
Line(P p1,P p2) {}
};
How can I design Line class so it can accept Point object of different data types?
请注意,模板 Line
接受不同类型的 Point
,但是一个特定的实例化例如 Line<int,int>
将只接受 Point<int,int>
并且它是一种完全不同的类型并且与 Line<int,double>
等无关。如果您想要一种类型的线接受不同的点,则需要更多。这实际上取决于您实际想要实现的目标。
Line
不需要那么多类型参数。
template <typename Point>
struct is_point : std::false_type {};
template <typename T, typename U>
struct is_point<Point<T, U>> : std::true_type {};
template <typename P1, typename P2>
class Line {
static_assert(is_point<P1>::value && is_point<P2>::value, "Line needs Point template parameters")
public:
Line(P1 p1, P2 p2) {}
};
我有一个模板class Point 将存储x和y坐标,两者的数据类型可以是任何算术类型。
template <typename T, typename U>
struct Point {
T x;
U y;
};
Line class 将两个 Points 作为构造函数参数来查找直线方程。
class Line {
public:
Line(Point p1, Point p2) {}
};
如何设计 Line class 使其可以接受不同数据类型的 Point 对象?
我设计了类似下面的东西,我该如何改进它?
template <class T, class U,
class = typename std::enable_if<std::is_arithmetic<T>::value &&
std::is_arithmetic<U>::value>::type>
struct Point
{
T x;
U y;
typedef T x_type;
typedef U y_type;
};
template <typename T1,
typename T2,
typename P1 = typename T1::x_type,
typename P2 = typename T1::y_type,
typename P3 = typename T2::x_type,
typename P4 = typename T2::y_type>
class Line {
public:
Line(Point<P1, P2> p1, Point<P3, P4> p2) {}
};
我打算使用的方式是:
Point<int, long> p1 (2, 5);
Point<short, double> p2 (77, 33.5);
Line<Point<int, long>, Point<short, double>> line_(p1, p2);
OR
Line<Point<int, short>, Point<float, unsigned>> eq({1, 1}, {2.3f, 2});
改进或缩短代码的方法有哪些。
模板只是模板,不是类型。除非您编写一个函数模板,否则您不能拥有一个将模板作为参数的函数,或者如果您的 Line
是您已经拥有的 class 模板,但它可以写得更简单一些:
template <typename T,typename U>
class Line {
public:
Line(Point<T,U> p1,Point<T,U> p2) {}
};
或者对点的类型进行参数化 Line
:
template <typename P>
class Line {
public:
Line(P p1,P p2) {}
};
How can I design Line class so it can accept Point object of different data types?
请注意,模板 Line
接受不同类型的 Point
,但是一个特定的实例化例如 Line<int,int>
将只接受 Point<int,int>
并且它是一种完全不同的类型并且与 Line<int,double>
等无关。如果您想要一种类型的线接受不同的点,则需要更多。这实际上取决于您实际想要实现的目标。
Line
不需要那么多类型参数。
template <typename Point>
struct is_point : std::false_type {};
template <typename T, typename U>
struct is_point<Point<T, U>> : std::true_type {};
template <typename P1, typename P2>
class Line {
static_assert(is_point<P1>::value && is_point<P2>::value, "Line needs Point template parameters")
public:
Line(P1 p1, P2 p2) {}
};