我想知道为什么隐式类型转换不适用于类模板上的外部运算符重载.这是工作的非模板化版本:
class foo
{
public:
foo() = default;
foo(int that)
{}
foo& operator +=(foo rhs)
{
return *this;
}
};
foo operator +(foo lhs, foo rhs)
{
lhs += rhs;
return lhs;
}
Run Code Online (Sandbox Code Playgroud)
正如所料,以下行正确编译:
foo f, g;
f = f + g; // OK
f += 5; // OK
f = f + 5; // OK
f = 5 + f; // OK
Run Code Online (Sandbox Code Playgroud)
另一方面,当类foo声明为这样的简单模板时:
template< typename T >
class foo
{
public:
foo() = default;
foo(int that)
{} …Run Code Online (Sandbox Code Playgroud)