1

I have the following code, in which I overload the member function test::f for both lvalue and rvalue to *this

#include <iostream>

struct test{
  void f() &{ std::cout << "lvalue"; }
  void f() &&{ std::cout << "rvalue"; }
};

int main(){
  test tst;
  tst.f(); // lvalue, outputs "lvalue"

  // make it a rvalue reference
  std::move(tst).f(); // rvalue, outputs "rvalue"
}

Everything works as expected. However, if I add const-ness to BOTH test::f(), such as

      void f() const &{ std::cout << "lvalue"; }
      void f() const &&{ std::cout << "rvalue"; }

the code doesn't compile anymore, spitting the error

error: call of overloaded 'f()' is ambiguous

What exactly is happening here? Why f suddenly becomes ambiguous? Is it because a rvalue can bind to a const reference also, and the reference collapsing comes into play, i.e. the type becomes && & -> &, i.e. lvalue? I don't really understand if this is the reason.

PS: just found out from @T.C. that the code compiles on clang no matter the const-ness of f. It seems to fail for const member functions only on g++ (4.8 / 4.9) (BUT compiles on g++4.10)

PPS: Case closed, question is indeed a dupe, it is indeed a g++ bug, see the link Overload resolution with ref-qualifiers

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Interesting, it compiles with [clang](http://coliru.stacked-crooked.com/a/94b2c62b6d43059e) but not g++. – T.C. Aug 06 '14 at 21:01
  • @T.C. hmmm, it is interesting. I don't know how I manage to come up with these examples, I'm just trying to polish up my C++ and end up coming with all these g++ vs clang++ issues recently :) – vsoftco Aug 06 '14 at 21:04
  • I think the overload resolution should be the same as `void f(test const&);` vs `void f(test const&&);`, and the latter should unambiguously win for rvalues. [over.match.funcs]/4-5 and [over.ics.rank]/3.1.3 – dyp Aug 06 '14 at 21:08
  • @dyp, yeah that's what I though, it should be a better match, however not for g++... – vsoftco Aug 06 '14 at 21:08
  • I tried your code, it compiles fine with g++-4.8.1 maybe you are using and "older" g++ version? – char8_t Aug 06 '14 at 21:10
  • Compiles and works with g++4.10 (w/ and w/o const). Probably a fixed compiler bug. – dyp Aug 06 '14 at 21:11
  • @TheMasterMaind, no, I'm using g++4.9.1 – vsoftco Aug 06 '14 at 21:12
  • @TheMasterMaind With or without the `const`? With the `const` [it doesn't compile](http://coliru.stacked-crooked.com/a/d921092510ba6a10) when I tested it on g++ 4.8.1. – T.C. Aug 06 '14 at 21:13
  • Without the const. Okay I have to say that I dont used my PC, I compiled it on my Android smartphone. – char8_t Aug 06 '14 at 21:15
  • @TheMasterMaind yeah without const it works, and I understand why. I didn't get why it doesn't WITH const. – vsoftco Aug 06 '14 at 21:15
  • OK, then I guess it is just a g++ bug that got fixed on g++ 4.10. – vsoftco Aug 06 '14 at 21:18
  • I just tested it with declaring with const. Just worked fine. Yeah maybe it's a bug in your revision of the gcc – char8_t Aug 06 '14 at 21:22
  • @TheMasterMaind what compiler are you using? It seems it does not compile on any stable g++ version, only on clang. See http://goo.gl/YreY1H – vsoftco Aug 06 '14 at 21:26
  • Im using g++-4.8.1 posix on my Android device – char8_t Aug 06 '14 at 21:31
  • OK, it is a bug in many g++ implementations then, just find out that the question has been asked before. Case closed :) – vsoftco Aug 06 '14 at 21:33
  • @vsoftco: please provide a link for future readers, thanks. – erenon Aug 06 '14 at 21:39
  • The link appears on the top of the page, but here it is again: http://stackoverflow.com/q/17130607/3093378 – vsoftco Aug 06 '14 at 21:42

0 Answers0