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 3341646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:44:48+00:00 2026-05-18T00:44:48+00:00

When implementing the non-throwing swap idiom, should I use throw() ? namespace A {

  • 0

When implementing the non-throwing swap idiom, should I use throw()?

namespace A
{
   struct B
   {
     void swap( B& other ) throw()
     { /* fancy stuff that doesn't throw */ }
   };

   void swap( B& lhs, B& rhs ) throw()
   { lhs.swap(rhs); }
}

namespace std
{
   template<>
   void swap( A::B& lhs, A::B& rhs ) throw()
   { lhs.swap(rhs); }
}

In particular I worry about putting the throw() specification on the specialization of std::swap.

Bonus question:
Is the answer different when using C++0x’s noexcept keyword?

  • 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-05-18T00:44:49+00:00Added an answer on May 18, 2026 at 12:44 am

    In C++03 you can put it there, but if it’s true that the fancy stuff doesn’t throw, it’s basically just documentation. It may or may not affect performance by adding the equivalent of try / catch(...) { std::unexpected(); } around calls to the function: it’s up to the implementation whether it can do it without affecting performance.

    If you’re planning to use the noexcept operator (5.3.7) in C++0x, then suddenly it becomes worth having non-throwing exception specifications, so that the operator gives the “right” answer. I don’t really know what the noexcept operator is for, but if there’s a clever generic use for it, for example algorithms that become more efficient when something is non-throwing, then I guess it’s going to become necessary to mark functions as non-throwing, to get whatever the benefit is.

    For example:

    void foo() noexcept;
    void bar();
    
    template <void(*FUNC)()>
    void generic_thing() {
        if (noexcept(FUNC()) {
            // this won't throw, perhaps we can somehow take advantage of that
            FUNC();
        } else {
            // this might throw
            FUNC();
        }
    }
    

    Old style exception specifications (dynamic-exception-specification) are deprecated in C++0x (and pointless in C++03).

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

Sidebar

Related Questions

I'm implementing a Non-Blocking HTTP server in Java and decided to use pure Java
Does anyone have experience with implementing OpenID on a non technical website? If you
I'm implementing a WCF web service based on WF. This web service consumes other
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
Implementing a 'sandbox' environment in Python used to be done with the rexec module
When implementing a needle search of a haystack in an object-oriented way, you essentially
When implementing Quicksort, one of the things you have to do is to choose
I implementing a EventQueue and get notified when AWTEvents are send. I wait till
When implementing a singleton in C++, is it better for GetInstance() to return a
I'm implementing a tagging system for a website. There are multiple tags per object

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.