Thread overview
is there a cleaner way to new a static sized array?
Feb 24, 2010
BCS
Feb 24, 2010
daoryn
Feb 24, 2010
BCS
Feb 24, 2010
FeepingCreature
Feb 24, 2010
BCS
Feb 25, 2010
FeepingCreature
Feb 26, 2010
BCS
Feb 25, 2010
grauzone
Feb 26, 2010
BCS
February 24, 2010
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?


-- 
... <IXOYE><



February 24, 2010
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?
> 
> 


I'm sorry, but I dont understand the purpose. Whats wrong with "T[27] array"?
Unless you want to use the heap but if so, why not just go with dynamic arrays? Whats the advantage of using static arrays on heap vs dynamic?

February 24, 2010
On 24.02.2010 05:16, 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?
> 
> 

The example you listed is the best way.

I'm sorry.
February 24, 2010
Hello FeepingCreature,

> On 24.02.2010 05:16, 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?
>> 
> The example you listed is the best way.
> 

As it happes, it dosn't work as shown:

auto o = New!(Object)(); // oops, typeof(o) == Object*

> I'm sorry.
> 
-- 
... <IXOYE><



February 24, 2010
Hello Daoryn,

> 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?
>> 
>
> I'm sorry, but I dont understand the purpose. Whats wrong with "T[27] array"?

I need to, under some cases, allocate it array out of a mem-mapped file and under others, allocate it off the heap.

> Unless you want to use the heap but if so, why not just go with
> dynamic arrays? Whats the advantage of using static arrays on heap vs
> dynamic?

I expect to have piles of arrays, all the same length where I know their length at compile time. In short, aside from the problem of allocating them, I have no reason /not/ to use static arrays.

-- 
... <IXOYE><



February 25, 2010
On 24.02.2010 22:19, BCS wrote:
> Hello FeepingCreature,
> 
>> On 24.02.2010 05:16, 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?
>>>
>> The example you listed is the best way.
>>
> 
> As it happes, it dosn't work as shown:
> 
> auto o = New!(Object)(); // oops, typeof(o) == Object*
> 
>> I'm sorry.
>>

I assumed that was what you wanted.

Maybe you should just special-case static arrays inside the function with static if.
February 25, 2010
On Tue, 23 Feb 2010 23:16:35 -0500, BCS <none@anon.com> 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?

Want to hear something funny?  In developing the array append patch to fix stomping, I worried about how memory would be allocated for something like this:

int *i = new int;

Simply because I was worried someone would do:

int[] x = i[0..1];
x ~= 4;

If this didn't do the right thing, my array stomping patch might make this do the wrong thing.

But as it turns out, the d compiler basically rewrites the line

int * i = new int;

to

int * i = (new int[1]).ptr;

Yep, that's exactly what you did :)  So basically your solution *is* the solution to use, because it's what the compiler would do if the syntax was allowed.

Some people have proposed recently on the digitalmars.D newsgroup that dynamic array allocation with the dimension in the brackets should actually allocate a statically-sized array on the heap.  The only allowed form for dynamic arrays would be:

int[] x = new int[](1);

Which is currently valid, but would be the only way to allocate dynamic arrays.

Although it looks uglier and does not conform to other languages (such as java or C#), it allows you to easily allocate a statically-sized array.  I hope this change goes through.

Note that you can special case your "New" template for objects using static if or using template constraints (if you are on D2)

-Steve
February 25, 2010
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)
February 26, 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><



February 26, 2010
Hello FeepingCreature,

> I assumed that was what you wanted.

Well my bad.

> 
> Maybe you should just special-case static arrays inside the function
> with static if.
> 

It's still ugly but it's what I'm going with for now.

-- 
... <IXOYE><