Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload resolution between template members in base and derived classes

Microsoft compiler (Visual Studio 2017 15.2) rejects the following code:

#include <type_traits>

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

struct D : B
{
    using B::f; 
    template<int n, std::enable_if_t<n == 1, int> = 0>
    void f() { }
};

int main()
{
    D d;
    d.f<0>();
    d.f<1>();
}

The error is:

error C2672: 'D::f': no matching overloaded function found
error C2783: 'void D::f(void)': could not deduce template argument for '__formal'
note: see declaration of 'D::f'

Clang also rejects it:

error: no matching member function for call to 'f'
    d.f<0>();
    ~~^~~~
 note: candidate template ignored: disabled by 'enable_if' [with n = 0]
    using enable_if_t = typename enable_if<_Cond, _Tp>::type;

GCC perfectly accepts it. Which compiler is right?

Addition:

With SFINAE in the form

template<int n, typename = std::enable_if_t<n == 0>>
...
template<int n, typename = std::enable_if_t<n == 1>>

GCC also produces an error:

error: no matching function for call to ‘D::f<0>()’
 d.f<0>();
        ^
note: candidate: template<int n, class> void D::f()
 void f()
      ^
note:   template argument deduction/substitution failed:
like image 906
Evg Avatar asked Aug 08 '17 19:08

Evg


People also ask

What is overload resolution?

The process of selecting the most appropriate overloaded function or operator is called overload resolution. Suppose that f is an overloaded function name. When you call the overloaded function f() , the compiler creates a set of candidate functions.

Can we overload derived class?

Since the constructors can't be defined in derived class, it can't be overloaded too, in derived class.

How we can overload the function template in C++?

You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.

What is overload resolution in C#?

Just as a reminder, overloading is what happens when you have two methods with the same name but different signatures. At compile time, the compiler works out which one it's going to call, based on the compile time types of the arguments and the target of the method call.


1 Answers

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) { }
};
like image 174
Jarod42 Avatar answered Oct 12 '22 15:10

Jarod42