Destructors, const structs, and opEquals

Don nospam at nospam.com
Fri Dec 3 22:42:06 PST 2010


Officially, opEquals has to have the signature:

struct Foo {
bool opEquals(const ref Foo x) const {...}
}

But this disallows comparisons with rvalues.
eg,

Foo bar() { Foo x = 1; return x; }
Foo y=1;
assert( y == bar() ); // doesn't compile

You can get around this by declaring a non-ref opEquals.
But this fails if Foo has a destructor.

If a struct has a destructor, it cannot be const(this is bug 3606)
---
struct S {
     ~this() {}
}

void main() {
     const S z;
}
---
bug.d(6): Error: destructor bug.S.~this () is not callable using 
argument types ()
-------
Likewise, it can't be a const parameter (this is bug 4338).
void foo(const S a) {}
It works to have it as a const ref parameter.

Everything will work if you declare a const ~this(), but that seems a 
little nonsensical. And you cannot have both const and non-const ~this().

I'm a bit uncertain as to how this is all supposed to work.
(1) Should temporaries be allowed to be passed as 'const ref'?
(2) If a struct has a destructor, should it be passable as a const 
parameter? And if so, should the destructor be called?



More information about the Digitalmars-d mailing list