命名空间范围中的运算符在全局范围中隐藏了另一个 [英] Operator in namespace scope hiding another in global scope
问题描述
这是编译器错误吗?
template <typename T>
T& operator++(T& t)
{
return t;
}
namespace asdf {
enum Foo { };
enum Bar { };
Foo& operator++(Foo& foo);
void fun()
{
Bar bar;
++bar;
}
} // end namespace asdf
int main()
{
return 0;
}
GCC 4.7错误消息是:
The GCC 4.7 error message is:
error: no match for 'operator++' in '++bar'
note: candidate is:
note: asdf::Foo& asdf::operator++(asdf::Foo&)
note: no known conversion for argument 1 from 'asdf::Bar' to 'asdf::Foo&'
如果注释掉该行,它将编译:
It compiles if you comment out the line:
Foo& operator++(Foo& foo);
推荐答案
不,这不是bug.考虑了三组并行的运算符.成员,非成员运算符和内建函数.
No that is not a bug. There are three parallel sets of operators considered. Members, non-member operators, and builtins.
通过普通的不合格+ ADL查找来查找非成员查找,而忽略所有类成员函数.因此,全局运算符被一个更近的词汇表隐藏(并且中间的成员函数不会隐藏其他非成员).
The non-member ones are looked up by normal unqualified+ADL lookup, ignoring all class member functions. Hence the global operator is hidden by a lexical more closer one (and an intervening member function wouldn't have hidden other non-members).
请注意,重载解析发生在 名称查找之后 1 ;在您的情况下,名称为operator++
,但是没有适当的重载.
Note that overload resolution takes place after name lookup1; in your case the name operator++
was found, but no appropriate overload.
如果Bar已被全局声明,和/或名称空间asdf中的其他运算符,则ADL(在前一种情况下)或普通的不合格查找(在后一种情况下)会将该运算符拖入.
If Bar had been declared globally, and/or the other operator in namespace asdf, ADL (in the former case) or ordinary unqualified lookup (in the latter case) would have dragged the operator in.
1 :Overload resolution (...) takes place after name lookup has succeeded.
(C ++标准)
这篇关于命名空间范围中的运算符在全局范围中隐藏了另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!