Reputation: 9432
The standard gives two construct functions in std::allocator<T>
for the placement new syntax underneath:
void construct( pointer p, const_reference val ); (1) (until C++11)
template< class U, class... Args >
void construct( U* p, Args&&... args ); (2) (since C++11)
1) Calls new((void *)p) T(val)
2) Calls ::new((void *)p) U(std::forward<Args>(args)...)
Whats the difference between 1) and 2) except from the fact that we forward all arguments to the constructor in 2) For what reason do we need 1)?
Imagine we would only have signature 2) , then passing the argument from the (non-existing) first one would result in calling:
::new((void *)p) T(std::forward<const_reference>val)
which should call the copy constructor T(val) anyway? Here, I am asking me whats the point of having the additional signature 1)?
There is a difference one calls new
the other one the global function ::new
Thanks for shining some light in to this :-)
Upvotes: 1
Views: 116
Reputation: 65620
The answer is in the excerpts you posted: one is pre-C++11 and one is post-C++11. construct
is best expressed by perfect-forwarding arguments rather than calling the copy-constructor, but perfect-forwarding is only available in C++11, so we had to make do with the first option before this could be a reality.
Upvotes: 1