Overload resolution between template members in base and derived classes

不想你离开。 提交于 2019-11-30 15:00:23

Turning cppleaner's comment into an answer:

From namespace.udecl#15.sentence-1:

When a using-declarator brings declarations from a base class into a derived class, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list, cv-qualification, and ref-qualifier (if any) in a base class (rather than conflicting)

Unfortunately, template parameter doesn't count and both f has empty parameter-type-list, are not const and no ref-qualifier.

Derived::f so hides Base::f.

gcc is wrong to accept that code.

So the way to fix it is by default argument (returned type doesn't count either):

struct B
{ 
    template <int n>
    void f(std::enable_if_t<n == 0>* = nullptr) { }
};

struct D : B
{
    using B::f; 
    template <int n>
    void f(std::enable_if_t<n == 1>* = nullptr) { }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!