On Saturday, 13 March 2021 at 15:44:53 UTC, frame wrote:
Maybe I don't get this right but why you don't just use the void[] as it is?

void[] mem = [i];
//...
return (cast(T[]) mem)[0];

This is how I exchange variable data between DLLs.

That works, and is more elegant than the OP's solution. (And, I didn't know you could do it that way, so thanks for sharing!)

However, it still adds an unnecessary second level of indirection for interfaces and classes. The simplest and most efficient solution is actually this:

    auto indirect = cast(void*) i; // i is of type I.
    // ...
    return cast(I) indirect;

Where `is(I : T*, T) || is(I == interface) || is(I == class)`. The constraint enforces that all types should be accessed through exactly one level of indirection, allowing the same code to handle both reference types and value types with maximum efficiency.

Reply via email to