On Thu, 04 Mar 2010 12:29:25 -0500, Clemens <[email protected]> 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

Reply via email to