Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload resolution of template methods with ref-qualifiers

I'm developing a container with compile-time access function using special type. I also want to have an access function using a number in order to implement ops for all elements. Thus I have something like this:

struct S
{
    template<int I> int& f();
    template<class Q> int& f();
};

I want to forbid access for temporary objects, so I add an overload for type-access:

struct S
{
    template<int I> int& f();
    template<class Q> int& f() &;
    template<class Q> int& f() && = delete;
};

But then I have a problem with msvc compiler:

<source>(4): error C2560: 'int &Test::f(void) &': cannot overload a member function with ref-qualifier with a member function without ref-qualifier

However both gcc and clang accept it. Who is right?

https://godbolt.org/z/4bmA2-

like image 510
Gena Bug Avatar asked Oct 17 '22 07:10

Gena Bug


1 Answers

MSVC is wrong here.

The relevant rule is [over.load]/2.3:

Member function declarations with the same name and the same parameter-type-list as well as member function template declarations with the same name, the same parameter-type-list, and the same template parameter lists cannot be overloaded if any of them, but not all, have a ref-qualifier ([dcl.fct]).

Here the function templates have different template parameters (int I and class Q), so this rule does not apply, and there is no other rule stoping them from overloading.

like image 53
xskxzr Avatar answered Nov 15 '22 10:11

xskxzr