I tried this
struct Foo(T)
{
align(8) void[T.sizeof] data;
}
but
static assert(Foo!(int).data.alignof == 8);
fails, telling me the alignment is still 1 instead of 8.
Why is this, and how do I fix it, so that it works for any arbitrary alignment that is a power of 2 (not just 8)?
Browsing the DMD source, it looks like
alignofdoesn't take into accountalignattributes.Here is where it is handled:
This converts a
.alignofexpression into asize_texpression with valuealignsize(), so let's look atalignsize()for a static array:It just gets the alignment of the element type (
void) in your case.voidis handled byTypeBasic::alignsize(), which just forwards toTypeBasic::size(0)Looking at how other types handle
alignof, it doesn't look likealignattributes are taken into account at all, but I could be wrong. It may be worth testing the alignment manually.