Storing interfaces as void[]

David Zhang straivers98 at gmail.com
Fri Mar 12 17:37:43 UTC 2021


I want to store interfaces as untyped void[], then cast them back 
to the interface at a later time. However, it appears to produce 
garbage values on get().

Is this even possible, and if so, what is happening here? The 
alternative would be a struct { CheckedPtr self; api_fns.... }

e.g.

void register(I)(I i) {
   auto mem = new void[](I.sizeof);
   memcpy(mem.ptr, cast(void*) i, I.sizeof);

   // CheckedPtr includes a hash of fullyQualifiedName
   map[i.get_name()] = CheckedPtr!I(mem.ptr);
}

I get(I)() {
   // basically cast(I) p
   return map[I.get_name()].as!I();
}


More information about the Digitalmars-d-learn mailing list