Domanda

Is there any way to disable a struct from being used as an lvalue (cannot be used as a variable)?

I've tried disabling constructors and opAssign, but in the end, you can still assign it from functions.

unittest
{
    //mystruct doesntCompile;
    mystruct shouldntCompile = makeMyStruct();
}


struct mystruct
{
    @disable this();
    @disable this(this);
    @disable void opAssign(ref mystruct);
    @disable void opAssign(mystruct);
    this(int dummyArgument){}
}

mystruct makeMyStruct()
{
    return mystruct(0);
}
È stato utile?

Soluzione

Is there any way to disable a struct from being used as an lvalue (cannot be used as a variable)?

No, it's not possible. Every type can have instances that are lvalues.

You may get better answers if you let us know what you are trying to achieve. Why do you want to prevent your struct from being used as an lvalue?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top