public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52185] New: Const member function may change the object for which the function is called.
@ 2012-02-09 16:41 lsoltysiak at gmail dot com
2012-02-09 16:45 ` [Bug c++/52185] " pinskia at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: lsoltysiak at gmail dot com @ 2012-02-09 16:41 UTC (permalink / raw)
To: gcc-bugs
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++; }
};
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/52185] Const member function may change the object for which the function is called.
2012-02-09 16:41 [Bug c++/52185] New: Const member function may change the object for which the function is called lsoltysiak at gmail dot com
@ 2012-02-09 16:45 ` pinskia at gcc dot gnu.org
2012-02-09 17:00 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-02-09 16:45 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52185
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-02-09 16:44:20 UTC ---
Comeau C++ does not error out either.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/52185] Const member function may change the object for which the function is called.
2012-02-09 16:41 [Bug c++/52185] New: Const member function may change the object for which the function is called lsoltysiak at gmail dot com
2012-02-09 16:45 ` [Bug c++/52185] " pinskia at gcc dot gnu.org
@ 2012-02-09 17:00 ` redi at gcc dot gnu.org
2012-02-09 17:12 ` redi at gcc dot gnu.org
2012-02-09 18:30 ` lsoltysiak at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-02-09 17:00 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52185
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-02-09 17:00:32 UTC ---
G++ is correct.
In A::fun() 'this' is 'const A*' but you can still change A::a1 through a
reference.
Compare:
int* p;
struct A {
int a1;
A() : a1(-1) { }
int fun() const { return ++*p; }
};
int main()
{
A a;
p = &a.a1;
return a.fun();
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/52185] Const member function may change the object for which the function is called.
2012-02-09 16:41 [Bug c++/52185] New: Const member function may change the object for which the function is called lsoltysiak at gmail dot com
2012-02-09 16:45 ` [Bug c++/52185] " pinskia at gcc dot gnu.org
2012-02-09 17:00 ` redi at gcc dot gnu.org
@ 2012-02-09 17:12 ` redi at gcc dot gnu.org
2012-02-09 18:30 ` lsoltysiak at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-02-09 17:12 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52185
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-02-09 17:11:40 UTC ---
[class.this] says, "In a const member function, the object for which the
function is called is accessed through a const access path;"
That doesn't mean the object is immutable.
In the member function you can't change A::a (but you couldn't do that anyway
because it's a reference) but you can change the thing it is bound to, because
it's a non-const reference, and it could point to a non-member just as easily
as to a member e.g.
int i = 0;
struct A {
int a1;
int a2;
A(bool b) : a1(1), a2(b ? a1 : i) { }
void f() const { ++a2; }
};
Should this fail to compile?
What if you only ever call the constructor with a 'false' argument?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/52185] Const member function may change the object for which the function is called.
2012-02-09 16:41 [Bug c++/52185] New: Const member function may change the object for which the function is called lsoltysiak at gmail dot com
` (2 preceding siblings ...)
2012-02-09 17:12 ` redi at gcc dot gnu.org
@ 2012-02-09 18:30 ` lsoltysiak at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: lsoltysiak at gmail dot com @ 2012-02-09 18:30 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52185
--- Comment #4 from xfg <lsoltysiak at gmail dot com> 2012-02-09 18:30:34 UTC ---
Ok, I understood. Pointers are also quite good examples, to explain that.
Thanks Jonathan.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-02-09 18:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-09 16:41 [Bug c++/52185] New: Const member function may change the object for which the function is called lsoltysiak at gmail dot com
2012-02-09 16:45 ` [Bug c++/52185] " pinskia at gcc dot gnu.org
2012-02-09 17:00 ` redi at gcc dot gnu.org
2012-02-09 17:12 ` redi at gcc dot gnu.org
2012-02-09 18:30 ` lsoltysiak at gmail dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).