重载解析不适用于运算符重载
Overload resolution not applicable to operator overloading
N4296:13.1/1 [over.load]
的标准说:
A program is ill-formed if it contains two such non-overloadable
declarations in the same scope. [ Note: This restriction applies to
explicit declarations in a scope, and between such declarations and
declarations made through a using-declaration (7.3.3). It does not
apply to sets of functions fabricated as a result of name lookup
(e.g., because of using-directives) or overload resolution (e.g., for
operator functions). —end note ]
#include <iostream>
namespace A
{
int foo(){ return 1; };
}
using namespace A;
int foo(){ return 1; }
int main(){ }
在 foo 未被 odr 使用之前,这将是格式正确的。这很清楚,这代表了我在引用中提供的注释的第一部分(大约 using-directive
)。但是我不知道标准对我在引用中强调的运算符函数的重载解析意味着什么。能举个例子吗?
namespace A {
struct Foo{};
}
namespace B {
struct Bar{};
}
namespace A {
int operator+(const A::Foo&, const B::Bar&);
}
namespace B {
char operator+(const A::Foo&, const B::Bar&);
}
int main() { A::Foo() + B::Bar(); }
ADL 找到 A::operator+()
和 B::operator+()
。这两个签名仅在 return 类型上不同。
N4296:13.1/1 [over.load]
的标准说:
A program is ill-formed if it contains two such non-overloadable declarations in the same scope. [ Note: This restriction applies to explicit declarations in a scope, and between such declarations and declarations made through a using-declaration (7.3.3). It does not apply to sets of functions fabricated as a result of name lookup (e.g., because of using-directives) or overload resolution (e.g., for operator functions). —end note ]
#include <iostream>
namespace A
{
int foo(){ return 1; };
}
using namespace A;
int foo(){ return 1; }
int main(){ }
在 foo 未被 odr 使用之前,这将是格式正确的。这很清楚,这代表了我在引用中提供的注释的第一部分(大约 using-directive
)。但是我不知道标准对我在引用中强调的运算符函数的重载解析意味着什么。能举个例子吗?
namespace A {
struct Foo{};
}
namespace B {
struct Bar{};
}
namespace A {
int operator+(const A::Foo&, const B::Bar&);
}
namespace B {
char operator+(const A::Foo&, const B::Bar&);
}
int main() { A::Foo() + B::Bar(); }
ADL 找到 A::operator+()
和 B::operator+()
。这两个签名仅在 return 类型上不同。