Pregunta

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);
}
¿Fue útil?

Solución

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?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top