Question

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);
}
Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top