Disabling struct destructor illegal?

Jim Balter Jim at Balter.name
Thu Jul 19 09:50:32 UTC 2018


On Thursday, 19 July 2018 at 08:50:15 UTC, RazvanN wrote:
> struct A
> {
>     int a;
>     @disable ~this() {}
> }
>
> void main()
> {
>     A a = A(2);
> }
>
> Currently, this code yields:
>
> Error: destructor `A.~this` cannot be used because it is 
> annotated with @disable
>
> I was expecting that disabling the destructor would make it as 
> if the struct does not have a destructor

Why? That's not the semantics of @disable. And why would you want 
that? What are you actually trying to achieve?

> , instead it makes the program not compile. I find this 
> behavior odd: why not make it illegal to disable the destructor 
> if disabling it will surely result in errors wherever the 
> struct is used.

Because it won't surely result in errors wherever the struct is 
used ... you yourself provide an example below where it doesn't.

  The only
> situation where the code will compile is A is never used 
> directly.

Eh? You immediately give a contrary example:

> To make matters even more confusing, this code compiles:
>
> class A
> {
>     int a;
>     @disable ~this() {}
> }
>
> void main()
> {
>     A a = new A();
> }

Why is that confusing? Why shouldn't it compile? The A that you 
created is on the heap, so its destructor is never invoked, so 
what would cause it not to compile?

>
>
> So, is this a bug or am I missing something?
>
> Yoroshiku onegaishimasu,
> RazvanN




More information about the Digitalmars-d-learn mailing list