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
alignof
doesn't take into accountalign
attributes.Here is where it is handled:
This converts a
.alignof
expression into asize_t
expression with valuealignsize()
, so let's look atalignsize()
for a static array:It just gets the alignment of the element type (
void
) in your case.void
is handled byTypeBasic::alignsize()
, which just forwards toTypeBasic::size(0)
Looking at how other types handle
alignof
, it doesn't look likealign
attributes are taken into account at all, but I could be wrong. It may be worth testing the alignment manually.