struct and default constructor

Szymon Gatner via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 10 03:23:56 PDT 2014


On Friday, 10 October 2014 at 09:58:54 UTC, Walter Bright wrote:
> On 11/27/2011 11:53 AM, deadalnix wrote:
>> I wonder why struct can't have a default constructor. TDPL 
>> state that it is
>> required to allow every types to have a constant .init .
>
> Having a .init instead of a default constructor has all kinds 
> of useful properties:
>
> 1. the default object is trivially constructable and cannot fail
>
> 2. an easy way for other constructors to not have overlooked 
> field initializers, so they get garbage initialized like in C++
>
> 3. generic code can rely on the existence of trivial 
> construction that cannot fail
>
> 4. dummy objects can be created, useful for "does this compile" 
> semantics
>
> 5. an object can be "destroyed" by overwriting it with .init 
> (overwriting with 0 is not the same thing)
>
> 6. when you see:
>     T t;
> in code, you know it is trivially constructed and will succeed
>
> 7. objects can be created using TypeInfo
>
>
> Default constructors are baked into C++. I can't escape the 
> impression that the desire for D default constructors comes 
> from more or less trying to write C++ style code in D.

Bit OT: What is The D code style then? It would be very useful 
for those coming from C++ to have a wiki/article on how to 
translate C++ idioms and practices to D. I too am writing D code 
like I do my C++ and often find myself puzzled (deterministic 
d-tors being perfect example). Missing default struct c-tor is 
also one of such examples - and adding opCall() feels hacky.




More information about the Digitalmars-d mailing list