Re: std::string exception safety guarantees?
Herve Bronnimann wrote:
For the life of me, I cannot find anything in the C++ standard that
says what the value of s is after this code, assuming that the resize
*does* throw an exception:
std::string s("Here is a short string");
std::string s1 = s;
try {
s.resize(s.max_size() + 1); // will throw length_error exception.
catch(...) {
}
So basically your question is whether std::string::resize is required to
offer the strong guarantee, right? No matter what argument you'd pass
to resize, right? (I assume that you're not particularly interested in
setting the size to s.max_size() + 1!)
The closest it comes to offering any guarantees is 21.3, para 3: "3
The class template basic_string conforms to the requirements for a
Sequence (23.1.1) and for a Reversible Container (23.1)." However,
the chapter 23 only says, on the topic of exceptions:
23.1, para 10: Unless otherwise specified (see 23.2.2.3 and 23.2.5.4)
all container types defined ***in this clause*** (emphasis mine) meet
the following additional requirements: - if an exception is thrown
by...
Martin Sebor wrote:
It looks like a simple editorial issue.
I also think that there's an editorial issue here. Especially because,
as you say, chapter 21, "Strings library", explicitly refers to the
requirements from 23.1 [container.requirements].
But still, if those requirements also apply to std::string, would that
sufficient to make sure that std::string::resize offers the strong
guarantee? 23.1, para 10 doesn't say anything about resize.
Anyway, I would say yes, it's worth an LWG issue!
Kind regards,
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]