Topic: deletion of const objects
Author: 100754.2730@compuserve.com (Martin Aupperle)
Date: 1996/08/07 Raw View
The DWP says in 5.3.5 "Delete":
" The operand shall have a pointer type." ... "It is unspecified
whether the deletion of an object changes its value."
Does that mean that it is unspecified whether the construct
const X* x = new X;
delete x;
is valid? I could not find any reference in the DWP that prohibits the
use of delete on objects treated as consts. But it is of course
forbidden to change the state of such an object.
Thanx - Martin
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/08/08 Raw View
100754.2730@compuserve.com (Martin Aupperle) writes:
>The DWP says in 5.3.5 "Delete":
>
> " The operand shall have a pointer type." ... "It is unspecified
>whether the deletion of an object changes its value."
>
>Does that mean that it is unspecified whether the construct
>
> const X* x = new X;
> delete x;
>
>is valid?
No. That construct is definitely valid (well-formed with well-defined
behaviour).
What it means is that for code such as
const X* x = new X;
const X* old_x = x;
delete x;
assert(x == old_x);
it is unspecified whether the assertion will hold or not.
>I could not find any reference in the DWP that prohibits the
>use of delete on objects treated as consts.
That's right, you're allowed to delete const objects.
That was illegal according to the ARM, but the committee
voted to allow it at the Nov '94 meeting.
>But it is of course forbidden to change the state of such an object.
Yes, but deleting the object doesn't change the state, it destroys it.
That's a different thing.
The ARM always allowed you to destroy const objects, giving an
inconsistent set of rules which allowed
const X *p = new X;
p->~X();
but disallowed
const X *p = new X;
delete p;
If it was forbidden to destroy a const object, then to be consistent
you'd also have to forbid leaving a scope containing the definition of
a const object! Of course, that would be rather unhelpful.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]