I have the following code:
#include "type_traits"
#include <new>
void foo()
{
std::aligned_storage<10,alignof(long)> storage;
new (&storage) int(12);
}
I have some storage defined (with length 10 bytes), and I am placement new
ing an int
into that location
gcc 7.3 gives the following warning:
<source>:10:10: warning: placement new constructing an object of type 'int' and size '4' in a region of type 'std::aligned_storage<10, 8>' and size '1' [-Wplacement-new=]
new (&storage) int(12);
If I am not incorrect this warning is incorrect. Am I missing something here or is this warning spurious?
std::aligned_storage
is a trait with a nested type
member. That type
is of the correct size and alignment. The trait itself may have no data members, and therefore will get the default object size, which is 1.
So the fix is simple:
std::aligned_storage<10,alignof(long)>::type storage;