 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Thomas J Guest
|
Posted: Sun Dec 04, 2005 7:47 pm Post subject: Why can operator delete be called on a const pointer? |
|
|
Hi!
I wondered when i saw a method like this:
void MyClass::selfDestruction() const
{
delete this;
}
void iAmNotEvil(MyClass const* pMyClass)
{
p->selfDestruction();
}
Why is this possible? When I call a const method i recommend nothing
being changend, and in this case the object is being deleted. In my
eyes this shouldn't be possible, maybe I overlooked something.
Greetings
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Mon Dec 05, 2005 12:25 am Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
* Thomas J:
| Quote: | Hi!
I wondered when i saw a method like this:
void MyClass::selfDestruction() const
{
delete this;
}
void iAmNotEvil(MyClass const* pMyClass)
{
p->selfDestruction();
}
Why is this possible? When I call a const method i recommend nothing
being changend, and in this case the object is being deleted. In my
eyes this shouldn't be possible, maybe I overlooked something.
|
Destruction is simply construction in reverse.
During construction and destruction the object is not const, even if it
is const in between these two end-points of the life cycle.
Consider
struct Ballooba {};
Ballooba const* const p = new Balloba;
This is much of the point of constness: a guarantee that at least via
this pointer the object won't be modified between initialization and
destruction.
But if you couldn't call delete with that pointer as argument, you
couldn't have a const dynamically allocated object: const would then
only be for locals, statics and members -- or for cast-ridden code...
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Dec 05, 2005 12:57 am Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
"Thomas J" <quicix (AT) googlemail (DOT) com> writes:
| Quote: | I wondered when i saw a method like this:
void MyClass::selfDestruction() const
{
delete this;
}
Why is this possible?
|
The reasoning is that const extends to the lifetime of the object
only. The lifetime of an object starts when its constructor has been
fully executed and ends when its destruction starts.
| Quote: | When I call a const method i recommend nothing
being changend, and in this case the object is being deleted. In my
eyes this shouldn't be possible, maybe I overlooked something.
|
You are in good company (not mine, though, but I'm sure you can live
with that) AFAICT. What you are asking seems to be a religious
question.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Mon Dec 05, 2005 10:34 am Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
"Thomas J" <quicix (AT) googlemail (DOT) com> writes:
| Quote: | Hi!
I wondered when i saw a method like this:
void MyClass::selfDestruction() const
{
delete this;
}
void iAmNotEvil(MyClass const* pMyClass)
{
p->selfDestruction();
}
Why is this possible?
|
For the same reason that a const object can be destroyed when it goes
out of scope. Ending the lifetime of an object is not considered a
mutating operation.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Mon Dec 05, 2005 12:27 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
Thomas J wrote:
| Quote: | When I call a const method i recommend nothing
being changend, and in this case the object is being deleted.
|
This is philosophical, but try to think this way: after deleting, you
have absolutely no possibility to check whether anything has changed in
the given object (you cannot access it), so you also have no possibility
to *prove* that its constness was violated. ;)
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Mon Dec 05, 2005 4:52 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
[email]maeder (AT) glue (DOT) ch[/email] (Thomas Maeder) wrote (abridged):
| Quote: | When I call a const method i recommend nothing
being changend, and in this case the object is being deleted. In my
eyes this shouldn't be possible, maybe I overlooked something.
You are in good company
|
In particular, the C++ Annotated Reference Manual agrees with the original
poster. It has some discussion in $5.3.4, which concludes:
Without the restriction against deleting pointers to const
objects the delete operator would have the effect of implicitly
removing the const attribute from a pointer. No operator
should have that property.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sektor van Skijlen Guest
|
Posted: Tue Dec 06, 2005 10:31 am Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
Dnia 5 Dec 2005 11:52:40 -0500, Dave Harris skrobie:
| Quote: | maeder (AT) glue (DOT) ch (Thomas Maeder) wrote (abridged):
When I call a const method i recommend nothing
being changend, and in this case the object is being deleted. In my
eyes this shouldn't be possible, maybe I overlooked something.
You are in good company
In particular, the C++ Annotated Reference Manual agrees with the original
poster. It has some discussion in $5.3.4, which concludes:
Without the restriction against deleting pointers to const
objects the delete operator would have the effect of implicitly
removing the const attribute from a pointer. No operator
should have that property.
|
This seems to be completely wrong for me.
The 'delete' operator is not just a normal operator, which receives normal
arguments. You could define, for example, operator +, which will take a pointer
to appropriate type and its variance must be saved.
In case of both 'new' and 'delete' operators, there is no direct passing from
the form of its call to its implementation. And it does not change anything
that the "operator delete" function receives void* type, which can accept any
other data pointer type. Such a conversion does not happen in this case, nor
does it happen in case of 'new' operator.
Similarly, we could ask, why 'new' operator converts implicitly from void* to
pointer to an object. Moreover, why there's no different versions for operator
new( void* ) and operator new( const void* ), selected according to if the
user requested "new Obj()" or "new const Obj()". Consequences in case of
different method of allocation, permissions for these operations etc., I give
up to your imaginary.
If we want to look deeper into this topic, we could also ask, why you can
destroy an object, which's destructor is not const. You can be pushed to an
angle seeing that making it so strict would make you also unable to delete the
object at all (unless you violate the variance with const_cast).
I'd prefer to stop this paranoia.
It's best to accept that the 'operator delete' function is called INTERNALLY
by the language runtime (from within delete expression) and no pointer
conversion rules apply here. This same rule applies to 'new' operator and
'operator new' function as well.
Note: you can still use 'operator delete' and 'operator new' functions as
well. But in such a case the rules of pointer conversion do apply, as they do
in case of any other function called directly (in particular, which's call
expression directly refers to definition of its interface).
Regards,
Sektor
--
// _ ___ Michal "Sektor" Malecki <sektor(whirl)kis.p.lodz.pl>
\ L_ |/ `| /^ ,() <ethourhs(O)wp.pl>
// _ | / _/ / C++ bez cholesterolu: http://www.intercon.pl/~sektor/cbx
"I am allergic to Java because programming in Java reminds me casting spells"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
benben Guest
|
Posted: Tue Dec 06, 2005 10:39 am Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
| Quote: | This is philosophical, but try to think this way: after deleting, you
have absolutely no possibility to check whether anything has changed in
the given object (you cannot access it), so you also have no possibility
to *prove* that its constness was violated. ;)
|
That said, it is bad practice to destroy const objects anyway. How do
you know the object is heap-allocated. If you can be certain, you should
be able to delete the non-const version anyway.
Ben
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sektor van Skijlen Guest
|
Posted: Tue Dec 06, 2005 1:12 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
Dnia 6 Dec 2005 05:39:58 -0500, benben skrobie:
| Quote: | This is philosophical, but try to think this way: after deleting, you
have absolutely no possibility to check whether anything has changed in
the given object (you cannot access it), so you also have no possibility
to *prove* that its constness was violated. ;)
That said, it is bad practice to destroy const objects anyway. How do
you know the object is heap-allocated. If you can be certain, you should
be able to delete the non-const version anyway.
|
I got a feeling that you are mixing two separate things. If you want to
perform 'delete' on a pointer to object, you must ensure that it is
appropriate (two things should be taken into consideration: whether the object
was allocated with 'new' rather than created by any other way, including
automatic on the stack, and whether the information provided for this pointer
is enough to destroy it correctly - that is, if it points to object's base
class, whether this base class is polymorphic with virtual destructor).
The pointer/reference variance (CV-q) does not mean anything in this case.
--
// _ ___ Michal "Sektor" Malecki <sektor(whirl)kis.p.lodz.pl>
\ L_ |/ `| /^ ,() <ethourhs(O)wp.pl>
// _ | / _/ / C++ bez cholesterolu: http://www.intercon.pl/~sektor/cbx
"I am allergic to Java because programming in Java reminds me casting spells"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Tue Dec 06, 2005 1:34 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
benben wrote:
| Quote: | This is philosophical, but try to think this way: after deleting, you
have absolutely no possibility to check whether anything has changed in
the given object (you cannot access it), so you also have no possibility
to *prove* that its constness was violated. ;)
That said, it is bad practice to destroy const objects anyway. How do
you know the object is heap-allocated. If you can be certain, you should
be able to delete the non-const version anyway.
|
Not necessarily. Consider:
const int *p = new const int(0);
There /is/ no non-const version to delete. (You could use const_cast
to get an int* to delete, and I think that is even required by the
standard to work. However I would be happy to be proved wrong, and
even it is legal it is certainly not moral.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Wed Dec 07, 2005 2:34 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
[email]martinfrompi (AT) yahoo (DOT) co.uk[/email] (Martin Bonner) wrote (abridged):
| Quote: | Not necessarily. Consider:
const int *p = new const int(0);
There /is/ no non-const version to delete.
|
In non-template situations you can add one, perhaps wrapped in a smart
pointer, like:
template <typename T>
class Const {
T *p;
public:
Const( T *p = new T() ) : p(p) {}
const T *operator->() const { return p; }
const T &operator*() const { return *p; }
void destroy() { delete p; }
};
void test1() {
Const<int> p = new int();
p.destroy(); // Replaces delete p.
}
The wrapper makes sure the non-const version does not escape - the only
(purported) non-const operation you can do to Const::p is delete.
I suspect the persuasive examples involve template code:
template <typename T>
void test2( T *p1 ) {
T *p2 = new T();
delete p2;
}
would be an error if instantiated with (const int). Despite that, I
personally agree with the ARM: the delete operator conceptually modifies
its argument. I'd gladly sacrifice test2() to get const-correctness back.
There are other ways to write it.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Wed Dec 07, 2005 2:36 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
[email]ethouris (AT) pl (DOT) wp.spamu.lubie.nie.inva[/email]lid (Sektor van Skijlen) wrote
(abridged):
| Quote: | In particular, the C++ Annotated Reference Manual agrees with the
original poster. It has some discussion in $5.3.4, which concludes:
Without the restriction against deleting pointers to const
objects the delete operator would have the effect of implicitly
removing the const attribute from a pointer. No operator
should have that property.
This seems to be completely wrong for me.
|
The premise here is that the delete operator effectively modifies the
object. Given that, I think the ARM reasoning is sound. (I only quoted a
small snippet of it.)
| Quote: | Similarly, we could ask, why 'new' operator converts implicitly from
void* to pointer to an object.
|
It can safely do that because it has constructed the object. Indeed,
that's its job.
| Quote: | Moreover, why there's no different versions for operator new( void* )
and operator new( const void* ), selected according to if the
user requested "new Obj()" or "new const Obj()".
|
Placement new conceptually modifies the memory it's given. So does
operator delete(). Even if the object has no user-defined state, the
compiler itself may need to modify it, eg to install a vtable pointer or
other book-keeping/debugging info.
I don't think anyone is asking for const overloads of these in this
thread. There's clearly a difference between the delete operator and
operator delete.
| Quote: | If we want to look deeper into this topic, we could also ask, why you
can destroy an object, which's destructor is not const.
|
Indeed, in a case like:
static const Object obj;
the const must be removed before the object's destructor is run. However,
it doesn't follow that the delete operator is what should do this job.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Fri Dec 09, 2005 3:13 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
Dave Harris wrote:
| Quote: | ethouris (AT) pl (DOT) wp.spamu.lubie.nie.invalid (Sektor van Skijlen) wrote
(abridged):
In particular, the C++ Annotated Reference Manual agrees with the
original poster. It has some discussion in $5.3.4, which concludes:
Without the restriction against deleting pointers to const
objects the delete operator would have the effect of implicitly
removing the const attribute from a pointer. No operator
should have that property.
This seems to be completely wrong for me.
The premise here is that the delete operator effectively modifies the
object. Given that, I think the ARM reasoning is sound. (I only quoted a
small snippet of it.)
|
This is a matter of whether to consider deleting as a kind of modifying.
I could argue against it because modifying x means (old x) != (new x)
and in the case of delete (new x) doesn't even exist and the inequality
doesn't hold. :)
(I say "I 'could'" because I mean that it is reasonable to argue so,
though I admit that other interpretations are also possible.)
| Quote: | If we want to look deeper into this topic, we could also ask, why you
can destroy an object, which's destructor is not const.
Indeed, in a case like:
static const Object obj;
the const must be removed before the object's destructor is run. However,
it doesn't follow that the delete operator is what should do this job.
|
Why not?
const Object* p = new const Object;
// what 'new' does:
// (a) creates an Object
// (b) labels it const
// (c) returns the const object
delete p;
// what 'delete' does:
// (c) receives the const object
// (b) removes its const label
// (a) destroys the Object
This symmetry seems perfectly reasonable to me.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Niklas Matthies Guest
|
Posted: Fri Dec 09, 2005 6:35 pm Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
On 2005-12-09 15:13, Seungbeom Kim wrote:
:
| Quote: | const Object* p = new const Object;
// what 'new' does:
// (a) creates an Object
// (b) labels it const
// (c) returns the const object
delete p;
// what 'delete' does:
// (c) receives the const object
// (b) removes its const label
// (a) destroys the Object
This symmetry seems perfectly reasonable to me.
|
One could argue that it is not reasonable for the same reason that
normally non-const implicitly converts to const but not vice versa.
-- Niklas Matthies
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paavo Helde Guest
|
Posted: Sat Dec 10, 2005 2:16 am Post subject: Re: Why can operator delete be called on a const pointer? |
|
|
"Thomas J" <quicix (AT) googlemail (DOT) com> wrote in news:1133719758.592458.17710
@g44g2000cwa.googlegroups.com:
| Quote: | Why is this possible? When I call a const method i recommend nothing
being changend, and in this case the object is being deleted. In my
eyes this shouldn't be possible, maybe I overlooked something.
|
Yes, you are mixing up the goal and the means to achieve the goal. The
const modifier lets you know that you don't have to worry about the object
state in the *subsequent* operations on that object - the object content
will (or at least should appear to) remain the same. In case of deletion
however there is no subsequent operation, thus no worry!
hth
Paavo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|