struct and default constructor

dcrepid via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 9 18:32:51 PDT 2014


On Sunday, 27 November 2011 at 19:50:24 UTC, deadalnix wrote:
> Hi,
>
> I wonder why struct can't have a default constructor...

I know this is an old thread, but I've run into this same problem 
recently and search yielded this result.

I myself have tried working around the default-constructor 
problem with things like

this(bool bInit = true)

- which of course doesn't get invoked with MyStruct(), even with 
@disable this.

This seems like a language limitation to me.  I have a few 
templated resource management objects that rely on RAII, but 
sometimes there's no real use for arguments, especially if a 
template defines what needs to be created.  But now I'm forced to 
both disable the default constructor AND to require unnecessary 
parameters. Why?  Another workaround is to use some object 
factory mechanism, but that's just extra code to maintain.

Also, apparently doing cleanup in class object destructors is a 
big no-no as resources that it may need to clean up could 
potentially already be destroyed, depending on when the GC kicks 
in and what objects are cleared by it. So no possibility of using 
objects as resource acquire/release mechanisms.

I assume using scoped with class objects will have a similar 
problem..


More information about the Digitalmars-d mailing list