Where can I find information on exception safety in the STL? I.e. which
methods on which types offer what level of exception safety.
Josuttis has a useful list of classes and methods but he fails to mention
common operations such as copy construction and assignment. I can't find any
mention of this in my copy of the standard at all, although according to
Josuttis exception safety guarantees are part of the standard.
A specific question, I've looked at the gcc 3.2 and the VC++.NET
implementation of list<T>::operator= and neither appears to be exception
safe. That is if an excpetion is thrown you could end up with a partially
copied list. This surprises me, but is it OK according to the standard?
john 5 5324
In article <be************@ID-196037.news.dfncis.de>, John Harrison
<jo*************@hotmail.com> wrote:
| A specific question, I've looked at the gcc 3.2 and the VC++.NET
| implementation of list<T>::operator= and neither appears to be exception
| safe. That is if an excpetion is thrown you could end up with a partially
| copied list. This surprises me, but is it OK according to the standard?
Yes, it is OK according to the standard. This is commonly called the
basic guarantee. In such a situation you are guaranteed that if an
exception is thrown, the list will remain in a self-consistent state.
No list invariants will be violated. No memory will be leaked.
In general, where the standard is silent, it is implying basic
exception safety.
It sounds like you are looking for strong exception safety: If an
exception is thrown, the list will be left in the same state as it was
before the operation began. This is also known as commit or rollback
semantics.
In general list::operator= does not have the strong guarantee. But
some implementations may offer exceptions to this standard defined
behavior in certain circumstances. For example, the Metrowerks
std::list<T>::operator= offers the strong guarantee if T::operator= is
guaranteed not to throw. And this is accomplished with no additional
expense of memory.
If you have a std::container and you need to assign it with the strong
guarantee, you can always do so at the expense of using more memory:
template <class C>
inline
C&
strong_assign(C& x, const C& y)
{
C(y).swap(x);
return x;
}
Make a local copy of the source, and then swap that with the target.
The copy may throw, since this is allocating memory, and calling
C::value_type's copy constructor. If it does throw, the tempory
container will clean up after itself. If it does not throw, then it
is swapped into the target which is a no-throw operation.
The strong assign is not "better" than the basic assign. It is simply
different. Sometimes you need the basic, sometimes you need the
strong. The basic assign will in general use less memory and thus be
faster.
--
Howard Hinnant
Metrowerks
"Howard Hinnant" <hi*****@metrowerks.com> wrote in message
news:060720030930125544%hi*****@metrowerks.com... In article <be************@ID-196037.news.dfncis.de>, John Harrison <jo*************@hotmail.com> wrote:
| A specific question, I've looked at the gcc 3.2 and the VC++.NET | implementation of list<T>::operator= and neither appears to be exception | safe. That is if an excpetion is thrown you could end up with a
partially | copied list. This surprises me, but is it OK according to the standard?
Yes, it is OK according to the standard. This is commonly called the basic guarantee. In such a situation you are guaranteed that if an exception is thrown, the list will remain in a self-consistent state. No list invariants will be violated. No memory will be leaked.
That surprises me because (according to Josuttis at least) list offers
strong exception safety on most other operations (insert for instance). I
guess this is so to avoid having to allocate extra memory.
In general, where the standard is silent, it is implying basic exception safety.
I still haven't been able to find the part of the standard that deals with
this. Could you point out where it is? It sounds like you are looking for strong exception safety: If an exception is thrown, the list will be left in the same state as it was before the operation began. This is also known as commit or rollback semantics.
In general list::operator= does not have the strong guarantee. But some implementations may offer exceptions to this standard defined behavior in certain circumstances. For example, the Metrowerks std::list<T>::operator= offers the strong guarantee if T::operator= is guaranteed not to throw. And this is accomplished with no additional expense of memory.
OK thanks, I'll take a look at the code. If you have a std::container and you need to assign it with the strong guarantee, you can always do so at the expense of using more memory:
template <class C> inline C& strong_assign(C& x, const C& y) { C(y).swap(x); return x; }
Make a local copy of the source, and then swap that with the target. The copy may throw, since this is allocating memory, and calling C::value_type's copy constructor. If it does throw, the tempory container will clean up after itself. If it does not throw, then it is swapped into the target which is a no-throw operation.
The strong assign is not "better" than the basic assign. It is simply different. Sometimes you need the basic, sometimes you need the strong. The basic assign will in general use less memory and thus be faster.
-- Howard Hinnant Metrowerks
john
On Sun, 06 Jul 2003 08:56:07 +0100, John Harrison wrote:
Even I am having a lot of trouble with the STL exception safety. I'd like
to know a thing. Suppose, I have a type whose destructor might throw. And
I make a list of that type. So, that would mean that even the dtor of the
list can throw. Am I correct?
To John: You can find a good tabular form of what you are looking for in
TC++PL, Special Edition, Pg 956. Basically, the Appendix E deals with
exception safety.
I kind of take it as a rule of thumb that dtors should not throw.
Now, here, it is mentioned that clear() has a nothrow guarantee, but
~list() has no such guarantee? What to infer from this? Should I assume
that since ~list() does verry little apart from calling clear(), it has
the same exception specifications as clear().
Regards,
-Dhruv.
"Alexander Terekhov" <te******@web.de> wrote in message
news:3F***************@web.de... John Harrison wrote: [...] To John: You can find a good tabular form of what you are looking for
in TC++PL, Special Edition, Pg 956. Basically, the Appendix E deals with exception safety.
Thanks, I'll seek that out, I only have the regular edition.
http://www.research.att.com/~bs/3rd_safe0.html
thanks, john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gary.Hu |
last post by:
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}
After ran the program,...
|
by: robinsand |
last post by:
Header File: car.h #if !defined CAR_H
#define CAR_H
enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize,
ctMiniVan, ctSUV };
class Car
{
public:
Car();
|
by: Sek |
last post by:
Hi Folks!
I was pondering over a code and noticed that exception handlers were
present in the private, protected as well as public methods. And,
ofcourse, public methods were calling priv/prot...
|
by: Zorro |
last post by:
The simplicity of stack unraveling of C++ is not without defective
consequences. The following article points to C++ examples showing the
defects. An engineer aware of defects can avoid...
|
by: jehugaleahsa |
last post by:
Hello:
As an avid reader of C++ books, I know a lot of programmers out there
are confronted with the challenge of exception safety.
For those who don't know what it is, it is writing code in...
|
by: Aarti |
last post by:
I was reading about exception safety in vectors and came across a
staement that says
The clear operation of vector guarantees a nothrow provided the copy
and assignment operator do not throw an...
|
by: George2 |
last post by:
Hello everyone,
How do you understand the Bjarne's comments about exception
specification? Especially, "not required to be checked across
compilation-unit" and "violations will not be caught...
|
by: Ioannis Vranos |
last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard,
something simple like defining a function as:
void somefunc(void) throw()
{
// ...
}
|
by: Vincent Jacques |
last post by:
Hello all,
I'm writing a class with value semantic, and I want its public interface
to provide the strong exception safety guaranty (if a public operation
fails for any reason, then an...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |