using swap to make assignment operator exception safe

Tech-Archive recommends: Speed Up your PC by fixing your registry



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?

[Code]
class A;
A& A::operator= (const A& a)
{
A temp;
temp = a; // exception may be thrown
swap (*this, temp);
return *this;
}
[/Code]


thanks in advance,
George

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
.



Relevant Pages

  • Re: HELP ME
    ... We need to insert/update data from temp table to staging tables. ... any User Define Exception to be handled and then use Raise Exception ... Above code template does not specify Commit statement. ...
    (comp.databases.oracle.misc)
  • Re: HELP ME
    ... We need to insert/update data from temp table to staging tables. ... any User Define Exception to be handled and then use Raise Exception ... Above code template does not specify Commit statement. ...
    (comp.databases.oracle.misc)
  • HELP ME
    ... We need to insert/update data from temp table to staging tables. ... any User Define Exception to be handled and then use Raise Exception ... Above code template does not specify Commit statement. ...
    (comp.databases.oracle.misc)
  • Re: on exception in SPL
    ... And I include an exception handler for the -206 error that results if the table were never created which ignores the error and continues. ... What I was trying to do was keep the proc from blowing up if the temp table already existed for some reason. ... -- trace nrows; ... ordate datetime year to day, ...
    (comp.databases.informix)
  • Re: Best way to handle exceptions with try/finally
    ... return temp ... exception as you can ... raise an exception on an unknown variableName. ... which in turn raises ValueError ...
    (comp.lang.python)