且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基类和派生类中的模板成员之间的重载解析

更新时间:2023-02-15 19:39:49

将cppleaner的注释转换为答案:

Turning cppleaner's comment into an answer:

来自 namespace.udecl#15.sentence-1


当using-declarator将基类的声明带入派生类时,派生类中的成员函数和成员函数模板将覆盖和/或隐藏成员函数和成员基类(而不是冲突)中具有相同名称,parameter-type-list,cv-qualification和ref-qualifier(如果有)的功能模板

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)

不幸的是,模板参数不计算在内,并且 f 都具有空的parameter-type-list,不是const,也没有ref限定符。

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

Derived :: f ,因此隐藏了 Base :: f

gcc

因此,解决该问题的方法是默认参数(返回类型也不计算在内):

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) { }
};