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?
c ++ c ++ 11 overload-resolution ref-qualifier
Orient
source share