Implicit conversion between structs?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Oct 31 13:45:42 PDT 2011


On 10/31/11 12:39 PM, Steve Teale wrote:
> On Mon, 31 Oct 2011 07:58:11 -0500, Andrei Alexandrescu wrote:
>
>> On 10/31/11 4:43 AM, Steve Teale wrote:
>>> Is there a way to do this. For example if I have
>>>
>>> struct A { int a, b, c; }
>>> struct B
>>> {
>>>      int p, q, r, s;
>>>      this(A a) { p = a.a; q = a.b; r = a.c; }
>>> }
>>>
>>> A a;
>>> B b;
>>> b = a;
>>
>> Use opAssign?
>>
>
> Joseph (aka Andrei),
>
> But if B is not mine to mess with?

In Soviet Russia everything belongs to the people so you can mess with 
anything.

> The two structs have a common type prefix. and D guarantees that there
> are predictable values for the remainder of the larger one, so the
> compiler could presumably figure it out. (Easy for me to say)

Well yes, so you could use memcpy, but then that will become a soft 
error when you change the structs involved. You could take .tupleof for 
a B object and access its first there members. Generally we could 
automate a structural cast any way we want but probably we should limit 
such to only when the names of the members are also identical.


Andrei


More information about the Digitalmars-d mailing list