Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:55:52+00:00 2026-06-07T12:55:52+00:00

I keep reading that swap() operation, like this for example: template<class T> void swap

  • 0

I keep reading that swap() operation, like this for example:

template<class T>
void swap (T &a, T &b)
{
  T temp (a);
  a = b;
  b = temp;
}

is problematic when we are dealing with Exception-safety.

What’s so wrong with it?
Furthermore, how can we solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:55:54+00:00Added an answer on June 7, 2026 at 12:55 pm

    In the general implementation, assuming that any operation of T can throw, you cannot provide the strong exception guarantee which implies leaving the state exactly as it was before the operation on the event of a exception. Even if each operation on T offers the strong exception guarantee:

    template<class T>
    void swap (T &a, T &b)
    {
      T temp (a);          // [1]
      a = b;               // [2]
      b = temp;            // [3]
    }
    

    If [1] throws, the input is left untouched, which is good. If [2] throws, and assuming the strong exception guarantee, the values are still untouched, which is good. But if it is [3] that throws, a has already been modified, which means that after the exception propagates up the stack, the caller will be left with a state that is neither the original nor the final states.


    EDIT: Furthermore, how can we solve it?

    There is no general solution. But in most cases you can provide a exception safe swap operation for your types. Consider a vector<T>, which internally manages it’s state through the use of three pointers (begin, end, capacity). The general swap above can throw (fail to allocate, the constructors for the internal T might throw…), but it is trivial to provide a no-throw swap implementation:

    template <typename T>
    class vector {
       T *b,*e,*c;
    public:
       void swap( vector<T>& rhs ) {
          using std::swap;
          swap( b, rhs.b );
          swap( e, rhs.e );
          swap( c, rhs.c );
       }
    //...
    };
    template <typename T>
    void swap( vector<T>& lhs, vector<T>& rhs ) {
       lhs.swap(rhs);
    }
    

    Because copying of pointers cannot throw, the swap above offers the no-throw guarantee, and if you always implement swap following the pattern above (using std::swap; followed by unqualified calls to swap) it will be picked up by ADL as a better match than std::swap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep reading how xhtml and other languages alike don't like CAPITAL letters. This
I keep reading that an ASP.NET based web site should have custom errors enabled
Lets say, for example, that I have a class that requires the use of
One of the benefits of ASP.NET MVC I keep reading about is that it's
Is the current Scala Eclipse plugin ( http://www.scala-lang.org/node/94 ) usable? I keep reading that
I keep reading that if the DLL is linked against another version of the
I keep reading that to make ajax requests safe, I need to make sure
I keep reading that I have to use svcUtil.exe, crmSvcUtil.exe and company with tons
I keep reading everywhere that when you ask for dependencies to be injected in
I keep reading that, in C, using pointer arithmetic is generally faster than subscripting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.