不同版本模板类的二元运算符

发布于 2024-12-20 13:08:36 字数 924 浏览 4 评论 0原文

我有模板类 Point2D,我需要为模板类的不同版本实现二元运算符。由于 operator+ 问题,此代码无法编译

template <class T>
class Point2D
{
public:
    T x;
    T y;
    Point2D(T _x=0,T _y=0):x(_x),y(_y)
    {
    }
    Point2D(Point2D& ob)
    {
        x=ob.x;
        y=ob.y;
    }
    template <class T1>
    Point2D(Point2D<T1>& ob)
    {
        x=ob.x;
        y=ob.y;
    }
};
template <class T>
Point2D<T> operator+(const Point2D<T>& ob1,const Point2D<T>& ob2)
{
    return Point2D<T>(ob1.x+ob2.x,ob1.y+ob2.y);
}
int main()
{
    Point2D<int> ob1(10,10);
    Point2D<double> ob2(20,20);
    Point2D<double> ob3=ob2+ob1;
    return 0;
}

我想启用此类功能 Point2Dob3=ob2+ob1; 但编译器无法推导出该运算符的正确版本 Point2D;运算符+(const Point2D& ob1,const Point2D& ob2) 我应该改变什么才能让它发挥作用?

I have template class Point2D and I need to implement binary operator for diiferent version of template class. This code doesn't compile because of problem with operator+

template <class T>
class Point2D
{
public:
    T x;
    T y;
    Point2D(T _x=0,T _y=0):x(_x),y(_y)
    {
    }
    Point2D(Point2D& ob)
    {
        x=ob.x;
        y=ob.y;
    }
    template <class T1>
    Point2D(Point2D<T1>& ob)
    {
        x=ob.x;
        y=ob.y;
    }
};
template <class T>
Point2D<T> operator+(const Point2D<T>& ob1,const Point2D<T>& ob2)
{
    return Point2D<T>(ob1.x+ob2.x,ob1.y+ob2.y);
}
int main()
{
    Point2D<int> ob1(10,10);
    Point2D<double> ob2(20,20);
    Point2D<double> ob3=ob2+ob1;
    return 0;
}

I want to enable such feature Point2D<double> ob3=ob2+ob1; but compiler can't deduce right version of this operator
Point2D<T> operator+(const Point2D<T>& ob1,const Point2D<T>& ob2)
What should I change for making it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2024-12-27 13:08:36

使用两个模板参数:

template <class T1, class T2>
Point2D<T1> operator+(const Point2D<T1>& ob1,const Point2D<T2>& ob2)
{
    return Point2D<T1>(ob1.x+ob2.x,ob1.y+ob2.y);
}

这是让编译器满意的最小更改。

但是,正如您所看到的,结果的类型使用 T1 作为模板参数类型进行实例化,这可能并不理想。因此,您需要选择最适合您工作的类型。这涉及某种元编程。

在 C++11 中,使用尾随返回类型非常简单:

template <class T1, class T2>
auto operator+(const Point2D<T1>& ob1,const Point2D<T2>& ob2) -> Point2D<decltype(obj1.x+obj2.x)>
{
    typedef decltype(obj1.x+obj2.x) R;
    return Point2D<R>(ob1.x+ob2.x,ob1.y+ob2.y);
}

在此处熟悉尾随返回类型:

Use two template parameters:

template <class T1, class T2>
Point2D<T1> operator+(const Point2D<T1>& ob1,const Point2D<T2>& ob2)
{
    return Point2D<T1>(ob1.x+ob2.x,ob1.y+ob2.y);
}

This is the minimal change to make the compiler happy.

However, as you can see, the type of the result is instantiated with T1 as template argument type, which may not be desirable. So you need to choose the best type which is desirable for your work. And this involves some sort of metaprogramming.

In C++11, it is really easy, with trailing return type:

template <class T1, class T2>
auto operator+(const Point2D<T1>& ob1,const Point2D<T2>& ob2) -> Point2D<decltype(obj1.x+obj2.x)>
{
    typedef decltype(obj1.x+obj2.x) R;
    return Point2D<R>(ob1.x+ob2.x,ob1.y+ob2.y);
}

Get familiar with trailing-return-type here:

烙印 2024-12-27 13:08:36

您需要使用两个不同的模板参数,例如:

template <typename T, typename U>
Point2D<T> operator+(const Point2D<T>& ob1, const Point2D<U>& ob2)
{
    return Point2D<T>(ob1.x+ob2.x,ob1.y+ob2.y);
}

或者使用 common_type

template <typename T, typename U>
Point2D<typename std::common_type<T, U>::type>
operator+(const Point2D<T>& ob1, const Point2D<U>& ob2)
{
    return Point2D<typename std::common_type<T, U>::type>(
        ob1.x+ob2.x,ob1.y+ob2.y
    );
}

You need to use two different template arguments, like:

template <typename T, typename U>
Point2D<T> operator+(const Point2D<T>& ob1, const Point2D<U>& ob2)
{
    return Point2D<T>(ob1.x+ob2.x,ob1.y+ob2.y);
}

Or with common_type:

template <typename T, typename U>
Point2D<typename std::common_type<T, U>::type>
operator+(const Point2D<T>& ob1, const Point2D<U>& ob2)
{
    return Point2D<typename std::common_type<T, U>::type>(
        ob1.x+ob2.x,ob1.y+ob2.y
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文