Explicit default constructor for structs
Benjamin Thaut
code at benjamin-thaut.de
Wed Apr 9 12:48:17 PDT 2014
Am 09.04.2014 21:02, schrieb Timon Gehr:
> On 04/09/2014 08:53 PM, Benjamin Thaut wrote:
>> Am 09.04.2014 20:33, schrieb Timon Gehr:
>>> On 04/09/2014 04:59 PM, Benjamin Thaut wrote:
>>
>>> Why not just:
>>>
>>> struct Foo{
>>> this(){
>>> // do stuff here
>>> }
>>> }
>>>
>>> void main(){
>>> Foo foo1; // error, no init value
>>> auto foo2=Foo(); // ok
>>> }
>>
>> Because then the user might think, that the default constructor gets
>> called implicitly by the compiler.
>
> This would be pointed out by the compiler. (i.e. it is an error instead
> of an implicit call.)
>
>> Just like in C++. But as that is not
>> the case the syntax should be different to indicate that a struct might
>> still be instaniated just using T.init.
>>
>> Kind Regards
>> Benjamin Thaut
>
> What would be an use case for a bypassable default constructor?
I don't know any use case. But AFAIK the reason there is no default
constructor for structs in D has to do something with exception safetey.
So whatever cases those are, they might need a bypassable default
constructor.
More information about the Digitalmars-d
mailing list