Example error for initializer?

Chris Miller chris at dprogramming.com
Wed Jun 21 08:50:50 PDT 2006


On Wed, 21 Jun 2006 11:53:26 -0400, Chris Nicholson-Sauls  
<ibisbasenji at gmail.com> wrote:

> Chris Miller wrote:
>> On Wed, 21 Jun 2006 17:01:11 -0400, Paolo Invernizzi   
>> <arathorn at NOSPAM_fastwebnet.it> wrote:
>>
>>> Hi all,
>>>
>>> In http://www.digitalmars.com/d/arrays.html under Pointer Arithmetic   
>>> there's:
>>>
>>> int[] def = [ 1, 2, 3 ];    // dynamic array of 3 ints
>>>
>>> but does not compile (dmd .0161):
>>>
>>>    variable xdelta.main.def is not a static and cannot have static   
>>> initializer
>>>
>>> Example error?
>>>
>>> ---
>>> Paolo Invernizzi
>>   DMD doesn't support it yet for non-static, non-const; here is a  
>> workaround:
>>     const int[] _def_init = [ 1, 2, 3 ];
>>    int[] def = _def_init;
>
> Or use a templated variadic function.  Should be factored out by the  
> optimizer and/or inliner (I would imagine).
>
> # T[] array (T) (T[] vals ...) { return vals; }
> #
> # int[] def = array(1, 2, 3);
>
> -- Chris Nicholson-Sauls

Make that
    T[] array (T) (T[] vals ...) { return vals.dup; }
or it refers to old stack memory.



More information about the Digitalmars-d-learn mailing list