Comment #0 by TeddyBear12311 — 2016-07-12T15:24:46Z
C++ has the ability to align all members of struct/class in a scope: e.g., #pragma pack(push,16)
Without this in D, it requires aligning each type manually. It may seem like trivial work, but it is not and posses serious problems when converting C code that uses scoped alignment.
A simple
align(n)
{
struct x { int a, b, c; }
}
would be identical do
align(n) struct x
{
align(n)
int a, b, c;
}
It should be a trivial enhancement. I'm only concerned about the member alignment.
Alternatives are
align(n)
{
struct x { int a, b, c; }
}
would be identical do
struct x
{
align(n)
int a, b, c;
}
or
malign(n)
{
struct x { int a, b, c; }
}
would be identical do
struct x
{
align(n)
int a, b, c;
}
pragma could be used
pragma(align(n))
struct x { int a, b, c; }
struct y { int a, b, c; }
pragma(align(m))
struct z { int a, b, c; }
would be identical do
struct x
{
align(n)
int a, b, c;
}
struct y
{
align(n)
int a, b, c;
}
struct z
{
align(m)
int a, b, c;
}
The responses that a simply search and replace are not acceptable. It is not robust, may quietly fail, etc. Adding by hand to every struct or member is also not acceptable because because it depends on size. (trivial for one struct but not so for 100 structs)
I believe this is not an enhancement but an oversight. It is required for large complex code unless man hours is not a factor.
Comment #1 by kinke — 2022-06-20T12:48:03Z
I'm not sure whether this hasn't worked in the past, but it now definitely does:
```
struct S {
align(4) {
byte a;
short b;
bool c;
}
ulong d;
}
static assert(S.a.offsetof == 0);
static assert(S.b.offsetof == 4);
static assert(S.c.offsetof == 8);
static assert(S.d.offsetof == 16);
```
Comment #2 by alphaglosined — 2022-06-20T12:50:39Z
(In reply to kinke from comment #1)
run.dlang.org reports this as working on all D compilers.
Comment #3 by alphaglosined — 2022-06-20T12:51:41Z
(In reply to Richard Cattermole from comment #2)
> (In reply to kinke from comment #1)
>
> run.dlang.org reports this as working on all D compilers.
Ugh I meant all dmd compilers.