TITLE: delete through a const pointer (part II)

(comp.lang.c++.moderated)

[ This is a second part to the topic of deleting via a pointer to
  a const object (yesterday's tip). -adc ]


PROBLEM: David Kastrup <dak@pool.informatik.rwth-aachen.de>

My ARM states in Section 5.3.4:
A pointer to constant cannot be deleted.

I'd think it stupid if that has not survived into the draft standard.


RESPONSE: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson), 8 Apr 96

Well, it has not survived into the draft standard; you may think it
stupid, but I don't, and after hearing the arguments both ways, most of
the C++ committee didn't.  It basically goes against the object/memory
model in the draft standard, in which there is no such thing as const
constructors or const destructors, and in which a const object becomes
read-only only after the completion of its constructor, and becomes
writable again on entry to the destructor.  `const' does not mean
that an object can never be destroyed; it just means that the object
cannot be modified during its lifetime.  You seem to think it should
mean `immortal'.

Retaining the prohibition would raise a number of difficult questions:

- If a pointer to constant could not be deleted, how would you go
  about deleting an object allocated with `new const Foo'?

- Shouldn't a template which contains `T* p = new T; delete p;' work
  even if T is `const int'?

- Wouldn't it be inconsistent to allow `void foo(const Foo *p) { p->~Foo(); }'
  but to disallow `void foo(const Foo *p) { delete p; }'?
