473,388 Members | 870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

using swap to make assignment operator exception safe

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
4 3260
George2 wrote:
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?
[snip]

Yes.

Note, however, that swap does not throw for built-in types and for standard
containers. Moreover, given a class

class SomeClass {

type_1 member_1;
type_2 member_2;
...

};

the obvious implementation of swap is

swap ( SomeClass & lhs, SomeClass & rhs ) {
swap( lhs.member_1, rhs.member_1 );
swap( lhs.member_2, rhs.member_2 );
...
}

And this implementation will not throw provided that the swap functions for
type_1, type_2, ... do not throw. Thus, in principle, swap could be
implemented as a no-throw operation for all types.

However, there is a catch: libraries and other code outside your control. If
type_3 is defined in some third-party library and does not provide a
no-throw swap, the argument breaks down and you might have to work around
the problems.

(Since the code for swap conforms to a uniform pattern, one has to wonder if
it would be a good idea to change the language so that the compiler will
generate a default swap function for each type very much like the compiler
provided copy-constructor and assignment operator.)
Best

Kai-Uwe Bux
Jan 8 '08 #2
George2 wrote:
>
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.  

The above code loops infinitely. The "temp = a"
line calls the assignment operator again.

The idiom you want is:

A& A::operator=( const T& other )
{
A temp( other );
swap(*this, temp );
return *this;
}

Your assumptions are wrong. The code is safe
regardless whether A's copy constructor or swap
throws exceptions as long as they are internally
exception safe.

Jan 8 '08 #3
On Jan 8, 2:53 pm, Ron Natalie <r...@spamcop.netwrote:
George2 wrote:
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.  

The above code loops infinitely. The "temp = a"
line calls the assignment operator again.

The idiom you want is:

A& A::operator=( const T& other )
{
A temp( other );
swap(*this, temp );
return *this;
}

Your assumptions are wrong. The code is safe
regardless whether A's copy constructor or swap
throws exceptions as long as they are internally
exception safe.
swap() should never throw any exceptions.

Probably he wanted to write:
A temp = a; // exception may be thrown
Jan 8 '08 #4
Ron Natalie wrote:
The idiom you want is:

A& A::operator=( const T& other )
{
A temp( other );
swap(*this, temp );
return *this;
}
I prefer the shoter version:
A& A::operator=(A src)
{
swap(*this, src);
return *this;
}

Should be semantically equivalent to your version.

--
Dizzy

Jan 8 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Davis King | last post by:
Can I assume that std::swap will not throw when it is used to swap std::string objects? In the standard it says that the swap() std::string member function runs in constant time. I didn't see...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
16
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
6
by: Jack White | last post by:
Does anyone know if an analogue to the "swap()" function found in C++ exists in C#. For those not familiar, let's say you have a function that loads some collection passed in by reference. If an...
1
by: ma740988 | last post by:
Consider the source snippet. # include <iostream> struct foo_struct { int odx ; int pdx ; foo_struct () : odx ( 0 ) , pdx ( 0 )
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
5
by: Weihui Shen | last post by:
Hello. Sometimes I see that some class constructors were implemented with assignment operator and I wanna know whether it is safe to do that or not?
16
by: George2 | last post by:
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...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.