1

gcc 接受以下代码,而 clang 拒绝它。

struct S
{
    struct Type
    {
    };
    operator Type()
    {
        return Type();
    }
};
void f(S& s)
{
    s.operator Type(); // error: unknown type name 'Type'
}

标准说Type是“在对象表达式的类中查找” S。似乎 gcc 包括S搜索中的成员,而 clang只考虑S它的基类而不考虑。哪个是对的?

C++ 工作草案 N3337 的相关引用:

3.4.5 类成员访问 [basic.lookup.classref]/7

如果 id-expression 是转换函数 ID,则首先在对象表达式的类中查找其转换类型 ID ,如果找到,则使用名称。否则在整个后缀表达式的上下文中查找它。

4

1 回答 1

1

In this particular case gcc is right. The lookup rule dictates that lookup for Type should be performed first in the context of the type of the object then in the context of where the expression is used. The standard even provides an example, that although not exact is similar to yours:

struct A { };
namespace N {
  struct A {
    void g() { }
    template <class T> operator T();
  };
}
int main() {
   N::A a;
   a.operator A(); // calls N::A::operator N::A
}

Lookup starts inside ::N::A where it finds the injected name A and resolves it to be ::N::A.

于 2013-08-14T21:39:30.953 回答