The call to the overloaded ref-qualifiers member function is ambiguous

I found strange behavior when compiling my code with g ++ ( gcc 4.8.1 and MinGW 4.8.2 with the -std=gnu++1y flag). In the spirit of SSCCE, I highlighted the following snippet:

 struct C { template< typename X > auto f(X &&) const & { ; } template< typename X > auto f(X &&) & { ; } template< typename X > auto f(X &&) && { ; } }; int main() { int i{}; #if 1 C{}.f(i); #endif #if 1 C c{}; cf(i); #endif return 0; } 

This gives an error:

 main.cpp: In function 'int main()': main.cpp:29:10: error: call of overloaded 'f(int&)' is ambiguous cf(i); ^ main.cpp:29:10: note: candidates are: main.cpp:6:5: note: auto C::f(X&&) const & [with X = int&] f(X &&) const & ^ main.cpp:11:5: note: auto C::f(X&&) & [with X = int&] f(X &&) & ^ main.cpp:16:5: note: auto C::f(X&&) && [with X = int&] f(X &&) && ^ 

But in the case of #if 1 and #if 0 , or #if 0 and #if 1 it compiles fine. In addition, if I replace all auto with void , then everything also compiles successfully.

Is this a mistake or just misleading?

+7
c ++ c ++ 11 overload-resolution ref-qualifier
source share
1 answer

g ++ 4.8.2 has the same problem with an even simpler ( Live at coliru ):

 struct A { auto f() & {} auto f() && {} }; int main() { A{}.f(); A a; af(); } 

despite the fact that the program is clearly correct. Apparently, this is a mistake in the interaction between ref-qualifiers and type inference of the return type: presumably, the deduction process deprives qualifiers from the implicit argument of the object before passing them to resolve the overload.

I reported this as bug GCC 60943 .

+4
source share

All Articles