On 3 December 2017 at 00:45, Timur Doumler <goo...@timur.audio> wrote:
> Given a type with new-extended alignment, such as
>
> struct alignas (8*alignof(double)) D { double d[8]; };
>
> is it legal to construct an object of type D by placement-new’ing it into
> memory that is not over-aligned to alignof(D)?
>
> char* buffer = new char[sizeof(D)];
> D* d = new(buffer) D;
In the most general sense, the answer is "maybe". If there are no
observable side-effects,
and nothing ever cares whether a live object is there, the
initialization itself is fine. However, note
that you never began the object's lifetime. See [basic.life]/1:
"The lifetime of an object of type T begins when:
— storage with the proper alignment and size for type T is obtained, and
— if the object has non-vacuous initialization, its initialization is complete,
"
If the storage doesn't have the proper alignment, the lifetime of the
object never began.
> Further, is it correct that the compiler *cannot* in general assume that a
> pointer to an object of type D will point to a memory location over-aligned
> to alignof(D)?
In the sense that the pointer can be invalid or null, yes.