3 ms·
Here's my exact chat log: what happens when you do pop_back() on an empty vector in C++? In C++, when you call the pop_back() function on an empty vec
by roel_v 4y ago
Here's my exact chat log:
what happens when you do pop_back() on an empty vector in C++?
In C++, when you call the pop_back() function on an empty vector, it will result in an std::out_of_range exception being thrown. The exception indicates that an element cannot be removed from an empty vector, as there is nothing to remove. To prevent the exception, it is recommended to check the size of the vector before calling pop_back(), to ensure that the vector is not empty.
can you quote the relevant C++ standard on that?
Sure! Here is the relevant excerpt from the C++ Standard (C++20, ISO/IEC 14882:2020):
quote the relevant passage from the standard
Here is the relevant excerpt from the C++ Standard (C++20, ISO/IEC 14882:2020):
23.6.11 Vector data members [vector.data]
-1- The member function pop_back() shall have no effect if the vector is empty.
23.6.6 Vector capacity [vector.capacity]
-1- void pop_back();
Requires: empty() == false.
Effects: If size() > 0, caller shall call erase(end() - 1).
Throws: Nothing unless erase() throws an exception.
21.5.5.5 vector<T>::pop_back() [vector.pop_back]
-1- Removes the last element in the vector.
Complexity: Constant time.
Throws: Nothing unless erase() throws an exception.
- aw1621107 4y agoPretty interesting output. Some notes from a cursory glance/crosscheck against the first post-C++20 draft [0]: It gets the ISO/IEC number correct [1]. However, the sections are wrong - Section 23.6 is about stream iterators. Sections 23.6.6 and 23.6.11 do not exist. - Section 21.5.5 is a synopsis of the <cuchar> header. Section 21.5.5.5 does not exist - Section [vector.data] exists and is numbered 22.3.11.4. It only describes the data() function. - Section [vector.capacity] exists and is numbered 22.3.11.3. It describes the capacity(), reserve(), shrink_to_fit(), swap(), and resize() functions. - pop_back() is generally described in Table 78 (Section 22.2.3 Sequence Containers, [sequence.reqmts]), and is expanded upon for std::vector in Section 22.3.11.5 Modifiers [vector.modifiers]. The wording is not correct either. - Searching "shall have no effect" returns no hits (though I'm searching in a PDF, so maybe the search just misses it?). - The standard wording for pop_back() in general is: "Effects: Destroys the last element. Preconditions: a.empty() is false". The Standard says nothing about exceptions. - For std::vector specifically, the Standard says "Effects: Invalidates iterators and references at or after the point of the erase. Throws: Nothing unless an exception is thrown by the assignment operator or move assignment operator of T. Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the assignment operator of T is called the number of time equal to the number of elements in the vector after the erased elements." [0]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4868.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n48... [1]: https://www.iso.org/standard/79358.html https://www.iso.org/standard/79358.html