Topic: new const T() ? [was: Delete through const pointer]
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1999/04/08 Raw View
"J.Barfurth" <techview@bfk-net.de> writes:
>BTW: MSVC6 (again - sorry) refuses to compile the expression 'new const
>T()'. Is this standard ?
The expression 'new const T()' is well-formed, and so MSVC6's behaviour
does not conform to the standard.
Here's some non-normative quotes to that effect.
| 5.3.4 - New [expr.new]
...
| [Note: the
| type-id may be a cv-qualified type, in which case the object created
| by the new-expression has a cv-qualified type. ]
| 7.1.5.1 - The cv-qualifiers [dcl.type.cv]
...
| -5- [Example:
...
| const int* ciq = new const int (3);
The normative wording also allows it -- see the syntax for new
expressions, new-type-id, and type-id.
--
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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "J.Barfurth" <techview@bfk-net.de>
Date: 1999/04/08 Raw View
Nathan Myers schrieb in Nachricht <7egn1k$4r1$1@shell7.ba.best.com>...
>
>J.Barfurth<techview@bfk-net.de> wrote:
>>Nathan Myers schrieb in Nachricht <7edokk$mgo$1@shell7.ba.best.com>...
>>>There is no such thing as a const object on the heap.
>>
>>Does this mean, that 'new' actually ignores top-level constness ? Like:
>> const T* p = new const T(); // *p is _actually_ non-const
>> const_cast<T*>(p)->change(); // results are well-defined
>
>Yes. There is no operator const_new(). Operator new() returns
>a non-const pointer which is passed to the constructor.
I was talking about new-expressions, NOT operator new(). For auto const
objects having c'tors and/or d'tors, the compiler reserves a piece of
non-const memory (i.e. no ROM) to pass to the constructor as well. Actual
constness applies to these objects only after construction and before
destruction.
Wouldn't the expression 'p = new const T()' result in the following
sequence:
void* memory= operator new(sizeof(T)); // maybe sizeof(T const) ?
c'tor call T::T(), passing static_cast<T*>(memory) as construction time
this-pointer
p = &'make object const'( *static_cast<T*>(memory));
where >>const T& 'make object const'(T&)<< would be the imagined operation
'activating' an object's constness after the constructor has finished. It
would be this 'operation', that would yield undefined behavior of the
inverse const_cast.
BTW: MSVC6 (again - sorry) refuses to compile the expression 'new const
T()'. Is this standard ? I couldn't find such a statement. IMHO it would be
inconsistent.
-- J rg
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]