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

Maxim Fomin maxim at maxim-fomin.ru
Thu Nov 29 04:10:05 PST 2012


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 ?
}


More information about the Digitalmars-d mailing list