【发布时间】:2015-12-18 09:43:01
【问题描述】:
考虑下面的代码
namespace std {
struct type1 {/* Definition */};
template <class T> constexpr T operator+(const type1& x1, const T& x);
template <class T> constexpr T operator+(const T& x, const type1& x1);
}
namespace other {
struct type2 {/* Definition */};
template <class T> constexpr T operator+(const type2& x2, const T& x);
template <class T> constexpr T operator+(const T& x, const type2& x2);
}
int main(int argc, char** argv)
{
std::type1 var1;
other::type2 var2;
auto x = var1 + var2; // What will happen here?
}
auto x = var1 + var2行会发生什么?这是一种已定义的行为吗?
这是否意味着标准库永远不会定义泛型运算符,只在一侧使用模板以避免冲突使用?
【问题讨论】:
-
至于“会发生什么”不如先试试看告诉我们吧?
-
@101010 他们不再是了
-
这纯粹是学术性的(不是说这有什么问题),还是你有一个用例?
-
@xophos 我想到的问题如下:标准委员会是否可以标准化二进制运算符重载,其中一侧是 std:: 类,另一侧是不受约束的模板类型?
-
@xophos 如果没有约束,我看不出如何提供有意义的实现。
标签: c++ c++11 operator-overloading std ambiguous