Struct with default ctor (Was: [dmd-beta] dmd 2.064 beta take 2)

Jonathan M Davis jmdavisProg at gmx.com
Sat May 18 00:17:18 PDT 2013


On Saturday, May 18, 2013 01:46:16 Igor Stepanov wrote:
> When programmer see constructor, he think that it will be
> evaluated each time when he create a new object.
> This code will seem obvious for any C++ coder.
> And this coder will be deceived.
> 
> If we'll add default constructor to structs, we'll must safe of
> illusion, that constructor is called any time when new object
> have been created. And reject all cases when we can't provide
> this illusion.

Also, I really don't think that there's much point in having a default 
constructor if it's not executed at runtime. If it's going to be known at 
compile time, then just default initialize all of the members for the init 
value. What would a default constructor buy you?

It seems to me that a default constructor is useful in cases where you want to 
do stuff at runtime and the result can differ between executions of the program 
and possibly between instantions of that object - e.g. grabbing the computer's 
current IP or setting up the hourglass to display (which is how the hourglass 
is done in MFC).

And given the advantages of knowing a type's default value at compile time, I 
just don't see how we can have it be determined at runtime. So, while I think 
that there are cases where a default constructor could be quite useful, I 
don't see how we could usefully add it to D given how D deals with default 
initialization and everything that it does with that.

- Jonathan M Davis


More information about the Digitalmars-d mailing list