This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of CD1 status.
Section: 126.96.36.199 [vector.capacity], 188.8.131.52 [string.capacity] Status: CD1 Submitter: Beman Dawes Opened: 2007-10-31 Last modified: 2016-02-10
Priority: Not Prioritized
View other active issues in [vector.capacity].
View all other issues in [vector.capacity].
View all issues with CD1 status.
A std::vector can be shrunk-to-fit via the swap idiom:
vector<int> v; ... v.swap(vector<int>(v)); // shrink to fit
or:vector<int>(v).swap(v); // shrink to fit
or:swap(v, vector<int>(v)); // shrink to fit
A non-binding request for shrink-to-fit can be made to a std::string via:
string s; ... s.reserve(0);
Neither of these is at all obvious to beginners, and even some experienced C++ programmers are not aware that shrink-to-fit is trivially available.
Lack of explicit functions to perform these commonly requested operations makes vector and string less usable for non-experts. Because the idioms are somewhat obscure, code readability is impaired. It is also unfortunate that two similar vector-like containers use different syntax for the same operation.
The proposed resolution addresses these concerns. The proposed function takes no arguments to keep the solution simple and focused.
To Class template basic_string 23.4.3 [basic.string] synopsis, Class template vector 24.3.11 [vector] synopsis, and Class vector<bool> 24.3.12 [vector.bool] synopsis, add:
To basic_string capacity 184.108.40.206 [string.capacity] and vector capacity 220.127.116.11 [vector.capacity], add:
Remarks: shrink_to_fit is a non-binding request to reduce capacity() to size(). [Note: The request is non-binding to allow latitude for implementation-specific optimizations. — end note]
[ 850 has been added to deal with this issue with respect to deque. ]