Is D a cult?

grauzone none at example.net
Mon Mar 8 05:00:46 PST 2010


Walter Bright wrote:
> grauzone wrote:
>> Again, I can't understand. Does the compiler rely that tuples have the 
>> same byte layout as structs or function arguments? I thought the 
>> compiler could just copy all fields. And the backend can already 
>> return multiple values, since it can return structs and static arrays.
> 
> A tuple and a struct composed of the same tuple should be interchangeable.
> 
> This doesn't work, because the alignment is different for different 
> circumstances.

Sorry for being dense, but again: what does alignment have to do with 
this? This works flawlessly, although there are different alignments 
involved:

import std.stdio;

struct A {
align(1):
     short a;
     int b;
}

struct B {
     short a;
     int b;
}

void main() {
     A x = A(1, 2);
     B y;
     y.tupleof = x.tupleof;
     writefln("%s", y); //printf B(1, 2)
     writefln("%s %s", A.b.offsetof, B.b.offsetof); //prints 2 4
}

You can assign the tuple from type A to B, even though the fields have 
different alignments. Obviously, the actual tuple type has to be the same.



More information about the Digitalmars-d mailing list