new T[size] vs .reserve
Namespace
rswhite4 at googlemail.com
Sat Feb 2 10:01:28 PST 2013
On Saturday, 2 February 2013 at 17:48:14 UTC, FG wrote:
> On 2013-02-02 17:36, Namespace wrote:
>> ubyte[] arr = new ubyte[4];
>> arr ~= 4; // desirable: [4, 0, 0, 0];
>
> Why not arr[0] = 4? What is so special about having 4 elements?
Example:
struct Color {
public:
ubyte[4] colors;
}
ubyte[] data = new ubyte[color_data.length * 4]; // enough storage
foreach (ref const Color col; color_data) {
data ~= col.colors;
}
Currently impossible, even with self indexing absolute annoying
and unnecessary complicated.
Sure you could do the same with .reserve but the point is that
you does not want more storage than color_data.length * 4.
Currently I use my own struct which reserve only
color_data.length * 4 memory and let the index at beginning to 0.
But I'm using D because it is nice and simple, so why I should
create my own data structure, and use ugly malloc, realloc and
free, for such needed thing if the language could and should do
that for you?
More information about the Digitalmars-d-learn
mailing list