Can pop_back() ever reduce the capacity of a vector? (C++)

前端 未结 3 1166
野的像风
野的像风 2020-12-20 16:07

According to the C++ standard, is std::vector::pop_back() ever allowed to reduce the capacity of the vector?

I am asking because I would like t

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 16:46

    The comments are not really addressing it. Clearly, if std::vector is prohibited to use anything except std::allocator, and if std::allocator is prohibited to be extended with additional methods, then resizing with same base address is impossible, which makes it impossible to decrease capacity because iterators would be invalidated.

    Closest info I could find about reallocation was a stackoverflow comment on Why is there no reallocation functionality in C++ allocators? saying

    "There is nothing stopping std::vector from doing that in some cases (e.g., it knows its using the standard allocator). The standard library is allowed to use knowledge of the underlying system. – KeithB Jun 23 '10 at 21:39" (no references mentioned, though)

    There have been submitted ideas for adding realloc to std::allocator, but they have both been rejected:

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1953.html

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2045.html

    The papers don't explicitly state that std::allocator is prohibited to extend std::allocator, though - they only state that it doesn't need to. They also don't explicitly state that std::vector is prohibited to use API calls to the underlying system... So no real info there either.

提交回复
热议问题