This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/52185] New: Const member function may change the object for which the function is called.
- From: "lsoltysiak at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 09 Feb 2012 16:41:32 +0000
- Subject: [Bug c++/52185] New: Const member function may change the object for which the function is called.
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52185
Bug #: 52185
Summary: Const member function may change the object for which
the function is called.
Classification: Unclassified
Product: gcc
Version: 4.7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: lsoltysiak@gmail.com
According to paper www.open-std.org/JTC1/SC22/WG21/docs/papers/2011/n3242.pdf (
class.this point 2), a const member function shall not modify the object for
which the function is called.
With basic cases it is true, so for below class g++ returns error ('error:
increment of member âA::aâ in read-only object').
class A {
int a;
public: int fun() const { a++; }
};
Problems start when class contains references. In below example, const member
function allows to change internal state of class A instances. This shouldn't
be allowed. In my opinion compilation should failed, but g++ 4.7.0 doesn't
report any problem.
class A {
int a1;
int & a2;
public:
A() : a2(a1) { }
int fun() const { a2++; }
};