This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/56821] New: Unable to overload with references to 'this'.
- From: "3dw4rd at verizon dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 02 Apr 2013 21:56:01 +0000
- Subject: [Bug c++/56821] New: Unable to overload with references to 'this'.
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56821
Bug #: 56821
Summary: Unable to overload with references to 'this'.
Classification: Unclassified
Product: gcc
Version: 4.9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: 3dw4rd@verizon.net
Created attachment 29783
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29783
This is a reference for 'this' toy.
Here is a busybox I wrote to play with the new feature in gcc-4.8.1 and trunk:
--------------------------------------------------------
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&&â
--------------------------------------------------------
I think this is gcc bug.