...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <new> struct S { void *p; S (); ~S (); }; void f() { const unsigned N = 32; alignas (S) unsigned char buffer[sizeof(S) * N]; S *sp = ::new (buffer) S [N]; // ... } |
Compliant Solution (Clang / G++)
The amount of overhead required by array new expressions is unspecified but should be documented by quality implementations. The following snippet outlines an example compliant solution that's is portable to the Clang and GNU GCC compilers. Porting it to other implementations requires adding similar conditional definitions of the overhead
constant. When an implementation does not document the overhead, assuming it 's is at least as large as twice the size of sizeof(size_t)
should typically be safe. To verify that the assumption is, in fact, safe, the compliant solution defines an overload of the placement new
operator new that accepts the buffer size as a third argument and verifies that it is at least as large as the total amount of storage required.
...