struct throws errors if it has a DTor

Maxim Fomin maxim at maxim-fomin.ru
Sat Dec 8 05:09:38 PST 2012


On Saturday, 8 December 2012 at 11:10:27 UTC, Namespace wrote:
> Why this code compiles without Dtor, but not when I have one?
>
> http://dpaste.dzfl.pl/2792b974

Reading compiler errors is a good idea:

(18-25):Error: function c739.Color.opAssign (Color p) is not 
callable using argument types (Color) const

Because you use didn't supply opAssign, default one is generated. 
It calls postblit and destructor for temporary. Once you define 
any of them, default opAssign cannot be const and because you 
assign static const structs in static constructor at lines 18-25, 
compiler issues errors.

Solution is to move initialization of static fields to compile 
time and use static struct initialization. In any case you know 
values of Black, Green, etc. so why would one initialize them at 
runtime? You can also turn them into enums.

http://dpaste.dzfl.pl/09572204


More information about the Digitalmars-d-learn mailing list