Re: What is this feature
On Feb 27, 10:41 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
On 27 Feb., 21:53, dp19...@gmail.com wrote:
On Feb 27, 3:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
parag_p...@hotmail.com wrote:
I just saw some code that
looks like this
(void*) new (h) className ( arg1, arg2 );
all by itself. Is this an accepted syntax. I have never seen somethi=
ng
similar to this.
If yes, how does it work?
For reference, it's (miss-)called "placement new". Be careful,
though, as people (including the standard) use this expression
with two different meanings: either for any new expression with
this syntax, and the operator new function it calls, or for the
particular instance of this syntax where the argument (here, h)
is a void*, or can be converted into a void*.
Without knowing what 'h' is, it's hard to go into details, but
It calls
className::operator new( size_t, decltype(h) )
, from which it receives a pointer to some memory in which it then
constructs an instance of 'className' using the constructor that
accepts the two arguments. Since the entire expression returns
a pointer to the newly created object, the code seems to discard it
(no assignment anywhere). The cast to 'void*' is extraneous, AFAICT.
Of course, questions of the nature "what does that code do" are
better addressed to those who wrote the code.
In other words it constructs an object within an existing buffer "h"
instead of allocating a new one.
Maybe! We do not know what h is, so that is one possibility.
Another one is that the memory is allocated from some special
arena.
Well, we do know what happens at one level. The compiler will
generate code to call "operator new( sizeof( className ), h )",
then call the constructor of className with the arguments arg1
and arg2 on the results. Beyond that:
-- there must be a declaration for the corresponding operator
new function somewhere in scope, either as a member function
of className or one of its bases, or in global scope,
-- if that function is declared with a throw() exception
specifier, the compiler will check for null before calling
the constructor, and
-- if the constructor exits via an exception, and there is a
corresponding placement delete in scope, it will be called
with the return value of the operator new function.
As for the question "is this an accepted syntax?", it's too
vague to answer. It's certainly acceptable to the compiler
(provided the corresponding operator new function has been
declared). For readers? It depends on the context: I would, in
fact, be very surprised not to find something similar in an
implementation of std::vector. In fact, in
std::allocator<>::construct(), in the standard vector, but
probably directly in the vector class in any pre-standard
implementations, which didn't use allocators. It was certainly
present in my pre-standard array classes. (Except that I didn't
bother with the cast to void*. Usually, in such cases, you're
ignoring the return value completely---some people insert a cast
to void (not void*) in such cases, to shut up various lint
tools.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34