如何使用+ =运算符同时实现标量和向量加法? [英] How to implement both scalar and vector addition using the += operator?

查看:101
本文介绍了如何使用+ =运算符同时实现标量和向量加法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Vector2D类,我认为使用+ =/+运算符可以实现向量加法和标量加法.

I'm working on a Vector2D class, and I think both vector addition and scalar addition make sense to be implemented with the +=/+ operators.

麻烦的是,我真的不知道如何解决这种明显的论点模棱两可的问题,这就是Clang所说的:

Trouble is, I don't really know how to work around this apparent argument ambiguity, here's what Clang says:

vector2d_test.cpp:17:16: error: use of overloaded operator
      '+=' is ambiguous (with operand types 'Vector2D<float>' and 'int')
        vector += 1;
        ~~~~~~ ^  ~~~~~~~
vector2d.hpp:34:18: note: candidate function
    Vector2D<T>& operator+=(const Vector2D<T>& other)
                 ^
vector2d.hpp:41:18: note: candidate function
    Vector2D<T>& operator+=(const T summand) const

这是两个功能:

Vector2D<T>& operator+=(const Vector2D<T>& other)
{
    x += other.x;
    y += other.y;
    return *this;
}

template <typename S>
Vector2D<T>& operator+=(const S summand) const
{
    x += summand;
    y += summand;
    return *this;
}

那么...你知道我能做什么吗?

So... any idea what I can do about this?

推荐答案

目前尚不清楚您要做什么. operator+= 除非它们是成员,否则您发布的功能是不合法的.而如果 他们是成员,您会遇到类似的事情:

It's not clear what you're trying to do. The operator+= functions you post aren't legal unless they are members. And if they are members, and you have something like:

Vector2D<float> v;
//   ...
v += 1;

Vector2D<float>::operator+=( Vector2D<float> const& ) 函数是不可调用的,因此不会有歧义.如果 这些函数不是成员,则应将其编写为:

the Vector2D<float>::operator+=( Vector2D<float> const& ) function isn't callable, and so there can be no ambiguity. If the functions aren't members, then they should be written:

template <typename T>
Vector2D<T>& operator+=( Vector2D<T>& lhs, Vector2D<T> const& rhs );
template <typename T, typename U>
Vector2D<T>& operator+=( Vector2D<T>& lhs, U rhs );

即使在这种情况下,也不能用rhs调用第一个 键入int,因此没有歧义.

Even in this case, the first cannot be called with an rhs of type int, so there is no ambiguity.

在您发帖的第二秒末,我错过了const. 这显然是您的错别字,但仍然没有改变 除非 ,否则您还可以进行一些隐式转换 Vector2D(这可能不是一个好主意);否则, 第一个版本仍然无法调用.例如,如果有 从intVector2D的隐式转换,您调用 在非常量Vector2D上的+=,则第一个重载为 更好地匹配隐式第一个参数(这将导致 this指针),因为它是完全匹配的,甚至没有 简历转换,但第二个功能更适合 第二个参数,因为模板实例化结果 完全匹配因此通话不明确.

I missed the const at the end of the second in your posting. This is an obvious typo on your part, It still doesn't change anything, unless you also have some implicit conversions to Vector2D (which is probably not a good idea); otherwise, the first version is still not callable. If there is, for example, an implicit conversion from int to Vector2D, and you call += on a non-const Vector2D, then the first overload is a better match for the implicit first argument (which results in the this pointer), since it is an exact match, without even a cv conversion, but the second function is a better match for the second argument, because the template instantiation results in an exact match. So the call is ambiguous.

这篇关于如何使用+ =运算符同时实现标量和向量加法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆