Reset all Members of a Aggregate Instance

Nordlöw via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 3 13:55:04 PST 2015


On Thursday, 3 December 2015 at 21:38:48 UTC, Chris Wright wrote:
> The terrible way is something like:
>
> void reset(Object o)
> in {
>   assert(!(o is null));
> }
> body {
>   auto p = cast(ubyte*)*cast(void**)&o;
>   auto ci = o.classinfo;
>   auto init = cast(ubyte)ci.init;
>   p[0..init.length] = init[];
>   if (ci.defaultConstructor) {
>     ci.defaultConstructor(o);
>   } else {
>     throw new Exception("no default constructor; object is in 
> invalid
> state");
>   }
> }

In what way is this better than my solution?


More information about the Digitalmars-d-learn mailing list