Copy a struct and its context

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 12 13:11:08 PDT 2016


On 09/10/2016 10:44 PM, Yuxuan Shui wrote:
> I recently noticed nested struct capture its context by reference
> (which, BTW, is not mentioned at all here:
> https://dlang.org/spec/struct.html#nested). And bliting a struct
> obviously doesn't do a deep copy of its context.
>
> So my question is, is there a way to deep copy the context of a struct?

Can you show a small example? This seems to work:

auto foo(int i) {
     struct S {
         int foo() {
             return i;
         }
     }

     return S();
}

void main() {
     auto s = foo(42);
     auto s_copy = s;
     assert(s.foo() == 42);
     assert(s_copy.foo() == 42);
}

Ali



More information about the Digitalmars-d-learn mailing list