Victor Bazarov wrote:
Kai-Uwe Bux wrote:
>[..] Do
I understand this correctly:
1. Any call to new (placement or not) will call an allocation
function and construct the object.
2. For the usual placement new, we do not want to allocate memory;
thus, we provide a special allocation function that intentionally
does nothing. When allocation is a null-op we are just left with the
construction of the object that any call to new will perform.
3. To add insult to injury, we need to include the header <new>
because that is where the 'allocation function that intentionally
does nothing' is declared.
I am not sure how you make this conclusion. The header <newdeclares
overloaded 'operator new' functions that take some special arguments
(along with the size). Those forms do not exist by themselves in the
language, they are part of the library. It really has nothing to do
whether the allocation function for placement new does anything.
I didn't (mean to) say that you need <new*only* to include functions that
don't do anything or that whether you have to include <newdepends on
whether the functions do something or not.
What I find strange is the round-about way of C++ to provide object
construction at a specified location. Here is what I figured from 5.3.4:
A placement new expression like
new ( some, args ) T ( some, other, args )
can be understood as performing two steps (given in pseudo-code):
1. void* pos = operator new( sizeof(T), some, args ).
2. construct T( some, other, args ) at pos.
Thus, placement new provides a hook to supply additional arguments to an
allocation function (5.3.4/11).
As a trick, for just constructing an object of a given location, the header
<newprovides the overload
void* operator new (std::size_t size, void* ptr) throw();
described in 18.4.1.3. And this operator new overload is deliberately
designed to just return ptr and perform no other action. Thus, if you do
new ( (void*) t_ptr ) T ( some, other args );
the above two steps translate to
1. void* pos = operator new( sizeof(T), (void*)t_ptr );
( equivalent to: void* pos = (void*) t_ptr; )
2. construct T( some, other, args ) at pos.
I consider that a pretty awkward way of going about construction of an
object. The compiler knows how to construct an object at a given location
anyway. This is nothing that is done by the operator new overload from
<new>; and in fact, one would expect (or hope) that step 1 in the above
sequence is optimized away. So, I find it strange that C++ does not offer a
way of constructing an object at a given location that does not involve an
allocation function to be imported from <new>.
It's like using delete with the deallocation function from 18.4.1.3/5-10 for
calling a destructor.
>Oh boy: I need to include the right header to get a function that does
nothing!
No, not at all. You need to include the right header for the right
'operator new' function declaration.
Yes, and 18.4.1.3/1-4 describes the right one for the line in question
new ( (void*) t_ptr ) T ( some, other args );
and this right one just returns its second argument and does nothing else.
>Why does it have to be that weird? More and more, I come to
think that we should just be able to call constructors as we can call
the destructor.
It's all in your head.
I would think, it was in the standard before it was in my head :-)
Best
Kai-Uwe Bux