The placement allocation function is described as follows (C++14 n4140 18.6.1.3):
void* operator new(std::size_t size, void* ptr) noexcept;
Returns: ptr
.
Remarks: Intentionally performs no other action.
20.10.7.6 table 57 describes aligned_storage<Len, Align>
thus:
The member typedef type
shall be a POD type suitable for use
as uninitialized storage for any object
whose size is at most Len and whose
alignment is a divisor of Align.
This implies that in your case, &storage
is suitably aligned for holding an object of type T
. Therefore, under normal circumstances1, all 4 ways you've listed of calling placement new
are valid and equivalent. I would use the first one (new (&storage)
) for brevity.
1 T.C. correctly pointed out in the comments that it is technically possible for your program to declare an overload of the allocation function taking a typename std::aligned_storage<sizeof(T), alignof(T)>::type*
, which would then be selected by overload resolution instead of the library-provided 'placement new' version.
I would say this unlikely in at least 99.999% of cases, but if you need to guard against that as well, use one of the casts to void*
. The direct static_cast<void*>(&storage)
is enough.
Also, if you're paranoid to this level, you should probably use ::new
instead of just new
to bypass any class-specific allocation functions.