4

Here is a busybox I wrote to play with the new feature in gcc-4.8.1+ (I think clang-2.9+ should do this too) for N2439 (ref-qualifiers for 'this'):

class Foo
{
public:
  Foo(int i) : _M_i(i) { }
  int bar() & { return _M_i /= 2; }
  int bar() const & { return _M_i; }
  int bar() && { return 2 * _M_i; }

private:
  int _M_i = 42;
};

int
main()
{
  Foo ph(333);
  ph.bar();

  const Foo ff(123);
  ff.bar();

  Foo(333).bar();
}

It looks to me reading the standard 8.3.5 that the three bar() methods should be overloadable. I get a linker error though:

[ed@localhost ref_this]$ ../bin/bin/g++ -std=c++11 -o ref_this ref_this.cpp
/tmp/ccwPhzqr.s: Assembler messages:
/tmp/ccwPhzqr.s:73: Error: symbol `_ZN3Foo3barEv' is already defined

If I comment out int bar() const & I am unable to resolve ff.bar();:

[ed@localhost ref_this]$ ../bin/bin/g++ -std=c++11 -o ref_this ref_this.cpp
ref_this.cpp: In function ‘int main()’:
ref_this.cpp:26:10: error: no matching function for call to ‘Foo::bar() const’
   ff.bar();
          ^
ref_this.cpp:26:10: note: candidates are:
ref_this.cpp:11:7: note: int Foo::bar() &
   int bar() & { return _M_i /= 2; }
       ^
ref_this.cpp:11:7: note:   no known conversion for implicit ‘this’ parameter from ‘const Foo’ to ‘Foo&’
ref_this.cpp:13:7: note: int Foo::bar() &&
   int bar() && { return 2 * _M_i; }
       ^
ref_this.cpp:13:7: note:   no known conversion for implicit ‘this’ parameter from ‘const Foo’ to ‘Foo&&’

Is this a gcc bug or part of the standard?

I'm not on my computer with clang on it but what does clang say?

Andy Prowl
  • 114,596
  • 21
  • 355
  • 432
emsr
  • 13,551
  • 6
  • 45
  • 59
  • 4
    [Shameless self-plug](http://stackoverflow.com/a/8610728/500104) for those who don't know what this is all about. – Xeo Apr 02 '13 at 21:46

1 Answers1

2

This feature is not supported by GCC up to version 4.8.0. It should be supported by GCC 4.8.1, which has not been officially released yet.

To the best of my knowledge, the only major compiler that supports reference qualifiers on member functions at the moment is Clang. As you can see from this example, your code compiles fine on Clang 3.2.

Andy Prowl
  • 114,596
  • 21
  • 355
  • 432
  • References for this was [just added](http://gcc.gnu.org/ml/gcc-patches/2013-04/msg00024.html) to g--4.8.1 and gcc trunk. I know the release is damp and not many people have it but it is out there. – emsr Apr 02 '13 at 21:42
  • Your LWS example show a bunch of errors. Is this gcc? Do you also have a clang version? – emsr Apr 02 '13 at 21:45
  • Thanks, I see it. I'll check out LWS. I think IDEone has pretty old gcc. – emsr Apr 02 '13 at 21:47
  • @emsr: Both IDEone and LWS have GCC 4.7.2 for C++11, although LWS also has a bunch of other C++ compilers. – Xeo Apr 02 '13 at 22:12
  • @Xeo Thanks, I'm glad they bumped up versions. I'll have to try some of my tests. – emsr Apr 02 '13 at 23:59
  • [gcc bug fixed](http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56821). It was a mangling issue. – emsr Apr 03 '13 at 03:35