is there a cleaner way to new a static sized array?

BCS none at anon.com
Thu Feb 25 17:40:08 PST 2010


Hello grauzone,

> BCS wrote:
> 
>> I need a function that works like the following:
>> 
>>> T* New(T)() { return new T; }
>>> 
>> But that also works with static arrays:
>> 
>>> auto i = New!(int)();
>>> auto a = New!(int[27])();
>> The cleanest solution I can think of is:
>> 
>>> T* New(T)() { return (new T[1]).ptr; }
>>> 
>> but that seems ugly. Any ideas?
>> 
> Does something like this work as expected?
> 
> T* New(T)() { static struct X { T x; } return &(new X).x }
> 
> (untested)

I'm going to guess it works, but it doesn't make anything much cleaner (and 
BTW, I think that static is unneeded).

-- 
... <IXOYE><





More information about the Digitalmars-d-learn mailing list