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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:32:22+00:00 2026-05-25T23:32:22+00:00

Imagine I’m writing some container template or something. And the time comes to specialize

  • 0

Imagine I’m writing some container template or something. And the time comes to specialize std::swap for it. As a good citizen, I’ll enable ADL by doing something like this:

template <typename T>
void swap(my_template<T>& x, my_template<T>& y) {
    using std::swap;
    swap(x.something_that_is_a_T, y.something_that_is_a_T);
}

This is very neat and all. Until I want to add an exception specification. My swap is noexcept as long as the swap for T is noexcept. So, I’d be writing something like:

template <typename T>
void swap(my_template<T>& x, my_template<T>& y)
    noexcept(noexcept(swap(std::declval<T>(), std::declval<T>())))

Problem is, the swap in there needs to be the ADL-discovered swap or std::swap. How do I handle this?

  • 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-25T23:32:23+00:00Added an answer on May 25, 2026 at 11:32 pm

    I think I would move it into a separate namespace

    namespace tricks {
        using std::swap;
    
        template <typename T, typename U>
        void swap(T &t, U &u) noexcept(noexcept(swap(t, u)));
    }
    
    template <typename T>
    void swap(my_template<T>& x, my_template<T>& y)
      noexcept(noexcept(tricks::swap(std::declval<T>(), std::declval<T>()))) 
    {
        using std::swap;
        swap(x.something_that_is_a_T, y.something_that_is_a_T);
    }
    

    Alternatively you can move the whole code up into tricks and delegate to there.

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

Sidebar

Related Questions

Imagine you want to animate some object on a WinForm. You setup a timer
Imagine the scene, you're updating some legacy Sybase code and come across a cursor.
Imagine I have a table with two columns, a primary key and some data.
Imagine I have one common functionality: series and number (string and integer) of some
Imagine a simple scenario with a WPF window containing a button and some clear
Imagine two servers (each within an own jvm process) which communicate using some form
Imagine you homebrew a custom gui framework that doesn't use windows handles (compact framework,
Imagine we have a program trying to write to a particular file, but failing.
Imagine an object you are working with has a collection of other objects associated
Imagine I have an function which goes through one million/billion strings and checks smth

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.