Time to kill T() as (sometimes) working T.init alias ?

deadalnix deadalnix at gmail.com
Thu Nov 29 10:25:32 PST 2012


On Thursday, 29 November 2012 at 12:10:06 UTC, Maxim Fomin wrote:
> On Thursday, 29 November 2012 at 10:41:46 UTC, Mehrdad wrote:
>> I'm just not understanding the whole "the default construction 
>> of a struct should be a compile time creature, not a runtime 
>> one".
>>
>>
>>
>> Don't you have to initialize the struct with zero's either way?
>>
>> So either way, you're going to have to initialize it... so no 
>> perf increase in any way. Why prevent the user from 
>> default-initializing it the way he wants to?
>
> Every type has a CT-known default initializer, even classes 
> have (null). If structures had a runtime one, this would break 
> code (especially templates and CTFE) which relies on knowing 
> something about constant default instance of a type at CT.
>
> extern bool foo();
>
> struct S
> {
>   int i;
>   this() {
>     i = foo() ? 1 : -1;
>   }
> }
> ---------
> S s;
> dosmth(s);
> ---------
> //somewhere in Phobos
>
> void dosmth(T) (T obj)
> {
>   T val; // is i 0, -1 or 1 ?
> }

Error, S has no default initializer and must be explicitely 
initialized.

What is complicated about that ?


More information about the Digitalmars-d mailing list