How to disambiguate this construction in a templated conversion operator?

后端 未结 2 1065
北恋
北恋 2021-02-13 20:40

After being confused why my code gave me an ambiguity error on GCC but no errors on Clang, I simplified the code. It can be seen below.

struct Foo
{
    // Foo(F         


        
2条回答
  •  执笔经年
    2021-02-13 21:27

    My best guess after some digging: I get the same error with the following code:

    struct Foo { Foo(int*) {} };
    
    struct Bar {    
       operator Foo(); // { return Foo{nullptr}; }
       /* explicit */ operator int*();
    };
    
    int main() { Foo f{Bar{}}; }
    

    And, when I uncomment the commented code, then the problem disappears. It seems to me that, in OP's original templated version, when implicit conversion is required from Bar to Foo, GCC only "instantiates" conversion operator declarations and then resolves overload before instantiating of their bodies.

    As for why explicit helps, it is because in the second case, there is one more conversion required (Barint* and then int*Foo).

提交回复
热议问题