469,927 Members | 1,322 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

using swap to make assignment operator exception safe

200 100+
Hello everyone,


The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant).

It used a temporary object "temp" in this sample, and assignment is made on a to temp ar first. Even if there is exception, the current this object's state is not corrupted.

My question is, the pattern works only if there is no exception thrown by swap function. If there are exception in swap function, the state of current object instance may still be corrupted (swap may invoke the assignment operator of member variables). Is my understanding correct?

Expand|Select|Wrap|Line Numbers
  1. class A;
  2. A& A::operator= (const A& a)
  3. {
  4.     A temp;
  5.     temp = a; // exception may be thrown
  6.     swap (*this, temp);
  7.     return *this;
  8. }
  9.  

thanks in advance,
George
Jan 8 '08 #1
16 1612
weaknessforcats
9,208 Expert Mod 8TB
Yep. You would have to try the swap and if an exception were thrown, then you would throw onw yourself to terminate the current object.
Jan 8 '08 #2
George2
200 100+
Thanks for your comments, weaknessforcats.


My question is not using swap itself, but whether or not the pattern I put above only works if swap is exception safe.

Yep. You would have to try the swap and if an exception were thrown, then you would throw onw yourself to terminate the current object.

regards,
George
Jan 9 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
And how would you know if the swap is exception safe???
Jan 9 '08 #4
George2
200 100+
Hi weaknessforcats,


I mean it (swap does not throw exception) should be assumed and as a prerequisite to use the pattern I mentioned in original question to make an exception safe assignment operator.

If swap throws exception, the pattern is not exception safe.

And how would you know if the swap is exception safe???

regards,
George
Jan 10 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
should be assumed
There is a crash coming....
Jan 10 '08 #6
George2
200 100+
Sorry weaknessforcats,


What do you mean crash? What is crashed and under what situation? :-)

There is a crash coming....

have a good weekend,
George
Jan 11 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
If you assume there are no exceptions and one comes along, you won't catch it. It will float roght up top the OS where your process dies with an unhandled exception.
Jan 11 '08 #8
George2
200 100+
Hi weaknessforcats,


If swap is implemented in user defined types, we can surely know whether or not it throws any exceptions or not. Right? :-)

So, no unknown and mysterious things.

If you assume there are no exceptions and one comes along, you won't catch it. It will float roght up top the OS where your process dies with an unhandled exception.

regards,
George
Jan 13 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
Nope.

There's to way to know for sure that there isn't a throw in there somewhere.
Jan 13 '08 #10
George2
200 100+
Hi weaknessforcats,


1. If the code is written by you, you can definietely find the answers.

2. If not, you can get from the API document.

Why you can not know what exceptions will be thrown?

Nope.

There's to way to know for sure that there isn't a throw in there somewhere.

regards,
George
Jan 14 '08 #11
weaknessforcats
9,208 Expert Mod 8TB
An API document won't detect exceptions. You are not compiling the document but instead are compiling someone's code. That person may or may not have told the API writer about an exception. Or, the exception as added later and not put in the document.

Besides, of you use this approach, you will need top reread the API document every time it is updated to see if anything changes- and even then you still could be lied to. BTW: The Windows SDK is updated monthly. Happy reading.

You can't even tell with your own code. Why? because your code that does not throw may call a function written by someone else that does. So checking your code for throws won't cut it either.

The key here is assume. If you assume no exceptions, then you may be in trouble. Maybe not. It's better to assume the worst and have a plan for it. Of course, that makes more work.

You can't even get around this using C, which has no exceptions, because Microsoft has implemented structured exceptions in their C code and you have to be able to handle those as well.
Jan 14 '08 #12
George2
200 100+
Thanks weaknessforcats,


An API document won't detect exceptions. You are not compiling the document but instead are compiling someone's code. That person may or may not have told the API writer about an exception. Or, the exception as added later and not put in the document.
For Windows native STL API (I mean the ones developed by Microsoft), whether or not exception will be thrown is 100 sure. And we can rely on those. Agree? :-)


regards,
George
Jan 17 '08 #13
weaknessforcats
9,208 Expert Mod 8TB
For Windows native STL API (I mean the ones developed by Microsoft), whether or not exception will be thrown is 100 sure. And we can rely on those. Agree? :-)
Nope. Check out structured exceptions.
Jan 17 '08 #14
George2
200 100+
Structured exceptions are H/W related.


Nope. Check out structured exceptions.

regards,
George
Jan 18 '08 #15
weaknessforcats
9,208 Expert Mod 8TB
Structured exceptions are H/W related.
Nope. Check out RaiseException(). There are MS keywords _try, _catch, and _final for these.

Start here: http://msdn2.microsoft.com/en-us/lib...57(VS.85).aspx.
Jan 18 '08 #16
George2
200 100+
Thanks weaknessforcats,


Great resource! I will learn how to use structured exception handling. :-)

Nope. Check out RaiseException(). There are MS keywords _try, _catch, and _final for these.

Start here: http://msdn2.microsoft.com/en-us/lib...57(VS.85).aspx.

have a good weekend,
George
Jan 19 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Davis King | last post: by
10 posts views Thread by Tony Johansson | last post: by
16 posts views Thread by Martin Jørgensen | last post: by
6 posts views Thread by Jack White | last post: by
1 post views Thread by ma740988 | last post: by
25 posts views Thread by indrawati.yahya | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.