www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is there a cleaner way to new a static sized array?

reply BCS <none anon.com> writes:
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><
Feb 23 2010
next sibling parent reply daoryn <wef sag.com> writes:
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?
Feb 24 2010
parent BCS <none anon.com> writes:
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><
Feb 24 2010
prev sibling next sibling parent reply FeepingCreature <default_357-line yahoo.de> writes:
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.
Feb 24 2010
parent reply BCS <none anon.com> writes:
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><
Feb 24 2010
parent reply FeepingCreature <default_357-line yahoo.de> writes:
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.
Feb 24 2010
parent BCS <none anon.com> writes:
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><
Feb 25 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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 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
Feb 25 2010
prev sibling parent reply grauzone <none example.net> writes:
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)
Feb 25 2010
parent BCS <none anon.com> writes:
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><
Feb 25 2010