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-
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With