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

  • Home
  • SEARCH
  • 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 9215181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:08:50+00:00 2026-06-18T02:08:50+00:00

The C++ standard guarantees that std::swap will throw no exception. However, what if an

  • 0

The C++ standard guarantees that std::swap will throw no exception. However, what if an object to swap throws an exception during swapping? Next, how should the caller find an exception has happened? and what measures should the caller take?

PS: It is very common that a constructor throws an exception.

struct A
{
    A(const A&)
    {
        throw 1;
    }

    A& operator =(const A&)
    {
        throw 2;
        return *this;
    }
};

int main()
{
    A a1, a2;
    std::swap(a1, a2); // An exception happened, but the caller doesn't know.
    // How to do here ???
}
  • 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-18T02:08:51+00:00Added an answer on June 18, 2026 at 2:08 am

    The C++ standard guarantees that std::swap will throw no exception.

    No, it doesn’t. See 20.2.2 or the reference. There are two noexcept specifications for the two std::swap overloads:

    template<class T> void swap(T& a, T& b)
    noexcept(noexcept(
        std::is_nothrow_move_constructible<T>::value &&
        std::is_nothrow_move_assignable<T>::value
    ))
    
    template<class T, size_t N>
    void swap(T (&a)[N], T (&b)[N])    
    noexcept(noexcept(swap(*a, *b)))
    

    When these conditions aren’t satisfied, std::swap can throw and you can catch it.


    In case of the class you have presented, the predicates std::is_nothrow_move_constructible and std::is_nothrow_move_assignable are false, so the instantiation std::swap<A> doesn’t have the no-throw guarantee. It’s perfectly legal to catch exceptions from this swap.

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

Sidebar

Related Questions

Does the standard guarantee that std::less<MyEnumType> will order MyEnumType as if a value of
If I have a union, C standard guarantees that the union itself will be
As the title says: Does the standard guarantee, that std::string::resize will not do anything,
Does the standard guarantee that order of equal elements will not change (eh, forgot
I believe that the C++ standard for std::sort does not guarantee O(n) performance on
Possible Duplicate: Does reading from stdin flush stdout? C++ Standard guarantees that all data
The C standard guarantees that an int is able to store every possible array
Does the C++ standard guarantee that the call c = std::min(f(x), g(x)); evaluates the
I know that the C++ standard explicitly guarantees the size of only char ,
The standard explicitly states that main has two valid (i.e., guaranteed to work) signatures;

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.