Consider the following function:
void shrink_string(std::string& str) {
if (str.size() >= 2) {
str.resize(str.size() - 2);
}
}
I wonder if this function can be declared noexcept (assuming C++11 standard)? I understand that the documentation does not declare .resize() as noexcept, but that's mostly because this method can be used both for growing and shrinking a string.
Obviously, shrinking can be trivially implemented by leaving the string's capacity constant and simply reducing the internal length; at the same time resize() may decide to reallocate anyways, in order to free up some memory -- but would it fall back to the trivial resizing if reallocation throws an error?