I'm trying to build a wrapper that will allow you to copy structs that have members that disabled copying. The idea is that copying these members will revert them to init.

This is what I have so far:

struct NoCopy(T) {
static assert( !hasElaborateDestructor!T, "NoCopy does not support type " ~ T.stringof ~ " with elaborate destructor" );

private:
    ubyte[T.sizeof] __bytes;

public:
    this(T val) {
        __bytes[] = (cast(const ubyte *)&val)[0..T.sizeof][];
    }

    @property ref inout(T) value() inout nothrow @trusted @nogc {
        return *cast(inout T*)__bytes.ptr;
    }

    this(this) {
        value = T.init;
    }

    void opAssign(NoCopy rhs) {
        value = T.init;
    }

    void opAssign(T rhs) {
        value = move(rhs);
    }

    alias this value;
}

I'm having problems with setting the initial value for the byte array. The technique I use in "value" does not work for initialization:

test.d(19): Error: cannot convert &S to const(ubyte*) at compile time

At first, I said "fine, the user will do it". That doesn't work, however. It doesn't matter who tries to do it, I cannot get the byte value of the type at compile time.

I tried using a union, but that, too, doesn't work.

I understand why the restriction is in place. What I'm wondering is whether there is any other solution, either to the init problem or to the overriding disable problem.

Thank you,
Shachar

Reply via email to