首页 > 解决方案 > clang 上的模棱两可的运算符重载

问题描述

我有下面的代码(尽可能简化),它无法用 clang 编译(我一直回去,3.4.1。是编译此代码的唯一版本):

// ////////////////////////////////////////////////
// Vector 3
template <typename T>
struct TVector3
{
    TVector3() { }

    template <class U>
    TVector3<T>& operator*=(const U ) { return *this; }

    //template <class U>
    //TVector3<T>& operator*=(const TVector3<U>& ) { return *this; }
};

// ////////////////////////////////////////////////
// Matrix 3x3
template <typename T>
struct TMatrix3
{
    TMatrix3() { }
};

template <typename T>
TVector3<T>& operator*=(TVector3<T>& v, const TMatrix3<T>& )
{
    return v;
}

// ////////////////////////////////////////////////

int main()
{
    TVector3<float> o;
    TMatrix3<float> m;
    o *= m; // the offending statement that triggers the compilation error
    return 0;
}

在其他主要编译器(gcc、MSVC、ICC)上它编译(https://godbolt.org/z/99nadcMM7)。

这里发生了什么 ?

据我所知,调用不应该是模棱两可的,矩阵重载应该基于最专业的模板函数的规则(根据这篇文章:C++ 模板化函数重载规则)。

标签: c++templatesoperator-overloading

解决方案


推荐阅读