A slice can lose capacity simply by calling a function

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 2 07:46:27 PDT 2015


On 05/02/2015 01:56 AM, Jonathan M Davis via Digitalmars-d-learn wrote:

 > I really don't think that it's reasonable in the general case to 
expect to
 > be able to guarantee that the capacity of a dynamic array won't change.

Yes, it is very different from other languages like C and C++ that 
talking about this behavior is worth it.

The elements are const, the slice is passed by value, but the reserved 
capacity disappears:

void foo(const(int)[] b)
{
     b ~= 42;
}

     a.reserve(milllllion);
     assert(a.capacity >= milllllion);

     foo(a);
     assert(a.capacity == 0);    // WAT

Ali



More information about the Digitalmars-d-learn mailing list