Ambiguous when two superclasses have a member function with the same name, but different signatures

后端 未结 5 1997
野的像风
野的像风 2021-01-31 03:52
struct A {
    void f(int x) {}
};

struct B {
    template void f(T x) {}
};

struct C : public A, public B {};

struct D {
    void f(int x){}
    te         


        
5条回答
  •  花落未央
    2021-01-31 04:28

    I believe the compiler prefers A::f (non-template function) over B::f for no reason.
    This seems to be a compiler implementation bug more than a implementation dependent detail.

    If you add following line, then compilation goes fine and the correct function B::f<> is selected:

    struct C : public A, public B { 
      using A::f; // optional
      using B::f;
    };
    

    [Funny part is that until the ::f are not brought into the scope of C, they are treated as alien functions.]

提交回复
热议问题