Operator cast, GCC and clang: which compiler is right?

后端 未结 1 664
清歌不尽
清歌不尽 2021-01-17 09:56

Consider the following code:

struct S {
    using T = int;
    operator T() { return 42; }
};

int main() {
    S s;
    S::T t = s;
    // Is the following          


        
相关标签:
1条回答
  • 2021-01-17 10:48

    I believe this is clang bug (submitted as #27807)

    From [basic.lookup.classref]:

    If the id-expression is a conversion-function-id, its conversion-type-id is first looked up in the class of the object expression and the name, if found, is used. Otherwise it is looked up in the context of the entire postfix-expression. In each of these lookups, only names that denote types or templates whose specializations are types are considered. [ Example:

    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
    }
    

    —end example ]

    In t = s.operator T();, T is first looked up in the class of S, which should find your typedef and hence end up calling operator int().

    0 讨论(0)
提交回复
热议问题