Feature suggestion: in-place append to array
Steven Schveighoffer
schveiguy at yahoo.com
Thu Mar 4 10:08:03 PST 2010
On Thu, 04 Mar 2010 12:29:25 -0500, Clemens <eriatarka84 at gmail.com> wrote:
> Steven Schveighoffer Wrote:
>
>> int[] a;
>> a.setCapacity(10000); // pre-allocate at least 10000 elements.
>
> I would prefer the name reserve(). It has precedent in the STL, and the
> method doesn't actually always set the capacity, only if the current
> capacity is less then the argument. Even then it may conceivably
> allocate more than was asked for.
>
> In other words, the name setCapacity suggests this will always succeed:
>
> a.setCapacity(n);
> assert(a.capacity == n);
>
> ...which is not the case as far as I can tell.
You are correct, setCapacity ensures that *at least* the given number of
elements will be available for appending.
I planned on making the function a property (but a bug would not allow
that), the original intended usage was:
a.capacity = 10000;
Reserve doesn't work in this context. Can you come up with a name that
does?
I'll bring up reserve (as a function) as an alternative on the phobos
mailing list, and see what people say. I kind of liked the setter/getter
idea, but you make a good point.
-Steve
More information about the Digitalmars-d
mailing list