This is not uncommon. The complexity of the language has increased so much over the years, accumulating multiple revisions to the C++ standard, that even the C++ standard itself can be at odds with itself, sometimes.
Let’s just see what the C++ standard itself says about two versions of the overloaded resize()
vector method. I happen to have a copy of N4860 handy which is, basically, the C++20 version, and while looking up what the C++ standard itself says about resize()
‘s exceptions, I found that the two resize()
overloads define their exception behavior as follows:
constexpr void resize(size_type sz);
// …
Remarks: If an exception is thrown other than by the move constructor
of a non-Cpp17CopyInsertable T there are no effects.// …
constexpr void resize(size_type sz, const T& c);
// …
Remarks: If an exception is thrown there are no effects.
That’s the only mention of exceptions in resize()
. I found nothing on a more general in vector
itself, nor in “Container Requirements”, there was some discussion of exception guarantees but none pertaining to the specific details of vector’s resize()
or reserve()
.
This is an obvious oversight. It’s fairly obvious that when it comes to exceptions that might be generated as a result of reallocations, both overloads should have the same exception behavior. The first overload’s description is lifted straight from reserve()
that just precedes it. It goes without saying that resize()
uses reserve()
to grow the vector’s capacity, when needed, and inherits its exception guarantees/behavior.
But the same thing must be true with the 2nd resize()
overload. The only difference between them is that one default-constructs new values when the vector grows and the other one copy-constructs. But in terms of exception behavior during reallocation they must be identical. The total overall difference, related to exceptions, between the two overloads is due to any exception differences between the value’s default constructor and/or its copy/move constructors.
My question is, from looking at this documentation, how do you get to that? > Maybe I missed something obvious.
No, you did not miss anything. The C++ standard itself has some gaps; not to mention 2nd-hand sources of documentation like the one you’re looking at.
You get where you want to go by studying everything about the class, template, or algorithm in question, understanding how it must work — i.e. the resize()
s inheriting certain parts of their behavior from reserve()
— and then drawing the inescapable inferences.
TLDR: it is what it is.