<div dir="ltr"><span style="font-size:12.8000001907349px">Is there a standard way to shallow copy objects? </span><div style="font-size:12.8000001907349px">eg:</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px"><div>class A{</div><div>  int* a;</div><div>  string b;</div><div>}</div></div><div style="font-size:12.8000001907349px"><div><br></div><div>unittest{<br></div><div>  auto a=new A;</div><div>  a.a=new int(1);</div><div>  a.b="asdf";</div><div><br></div><div>  auto b=new A;<br></div><div>  b.shallowCopyFrom(a);</div><div>  assert(b.a==a.a);</div><div>  assert(b.b==a.b);</div><div>}<br></div></div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">How about:</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">option A1:</div><div style="font-size:12.8000001907349px"><div>void shallowCopyFrom(T)(T a, T b) if(is(T==class)){</div><div>  size_t size = b.classinfo.init.length;</div><div>  (cast(void*) a)[8 .. size] = (cast(void*) b) [8 .. size];</div><div>}</div></div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">Is this guaranteed correct?</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">option A2:</div><div style="font-size:12.8000001907349px"><div>auto shallowCopyFrom(T)(T a, T b) if(is(T==class)){</div><div>  foreach (name; __traits(allMembers, T)) {</div><div>    enum temp="a."~name~"="~"b."~name~";";</div><div>    static if(__traits(compiles, mixin("{"~temp~"}"))) {</div><div>      mixin(temp);</div><div>    }</div><div>  }</div><div>}</div></div><div style="font-size:12.8000001907349px">option A2 has issues with property, though.</div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">option A1 should also be adaptable to swapping contents.</div></div>