The case of &T.init

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue May 19 11:52:46 PDT 2015


On 5/19/15 2:40 PM, Marco Leise wrote:
> I was wondering why T.init is treated as a manifest constant.
> Don't all .init's end up in some data segment with a
> resolvable address ?

Because T.init IS a manifest constant. In other words, doing:

T t = T.init;

Generates code to initialize t. It doesn't copy the data from some init 
value stored in ROM.

Note, there is a place T.init is stored, and that is in typeid(T).init. 
However, it's not always there (could be null if T.init is all zero).

What we could do, however, is make one copy of an addressable T.init:

template initialValue(T)
{
    static immutable T initialValue = T.init;
}

that was used wherever it's needed, instead of creating a new one for 
every time you need it.


> Likewise it would be nice to get the a class's .init[] slice
> at compile-time.
>

I think you can:

typeid(SomeClass).init

-Steve


More information about the Digitalmars-d mailing list