digitalmars.D.learn - is there a cleaner way to new a static sized array?
- BCS (6/10) Feb 23 2010 The cleanest solution I can think of is:
- daoryn (3/19) Feb 24 2010 I'm sorry, but I dont understand the purpose. Whats wrong with "T[27] ar...
- BCS (8/29) Feb 24 2010 I need to, under some cases, allocate it array out of a mem-mapped file ...
- FeepingCreature (3/19) Feb 24 2010 The example you listed is the best way.
- BCS (5/25) Feb 24 2010 As it happes, it dosn't work as shown:
- FeepingCreature (3/30) Feb 24 2010 I assumed that was what you wanted.
- BCS (5/10) Feb 25 2010 It's still ugly but it's what I'm going with for now.
- Steven Schveighoffer (30/38) Feb 25 2010 Want to hear something funny? In developing the array append patch to f...
- grauzone (4/20) Feb 25 2010 Does something like this work as expected?
- BCS (5/26) Feb 25 2010 I'm going to guess it works, but it doesn't make anything much cleaner (...
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
BCS Wrote:I need a function that works like the following: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?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?
Feb 24 2010
Hello Daoryn,BCS Wrote:I need to, under some cases, allocate it array out of a mem-mapped file and under others, allocate it off the heap.I need a function that works like the following:I'm sorry, but I dont understand the purpose. Whats wrong with "T[27] array"?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?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
On 24.02.2010 05:16, BCS wrote:I need a function that works like the following:The example you listed is the best way. I'm sorry.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?
Feb 24 2010
Hello FeepingCreature,On 24.02.2010 05:16, BCS wrote:As it happes, it dosn't work as shown: auto o = New!(Object)(); // oops, typeof(o) == Object*I need a function that works like the following:The example you listed is the best way.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.-- ... <IXOYE><
Feb 24 2010
On 24.02.2010 22:19, BCS wrote:Hello FeepingCreature,I assumed that was what you wanted. Maybe you should just special-case static arrays inside the function with static if.On 24.02.2010 05:16, BCS wrote:As it happes, it dosn't work as shown: auto o = New!(Object)(); // oops, typeof(o) == Object*I need a function that works like the following:The example you listed is the best way.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.
Feb 24 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><
Feb 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: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) -SteveT* 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?
Feb 25 2010
BCS wrote:I need a function that works like the following:Does something like this work as expected? T* New(T)() { static struct X { T x; } return &(new X).x } (untested)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?
Feb 25 2010
Hello grauzone,BCS wrote:I'm going to guess it works, but it doesn't make anything much cleaner (and BTW, I think that static is unneeded). -- ... <IXOYE><I need a function that works like the following:Does something like this work as expected? T* New(T)() { static struct X { T x; } return &(new X).x } (untested)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?
Feb 25 2010