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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:27:55+00:00 2026-05-30T01:27:55+00:00

Background Consider the following code: #include <utility> namespace ns { struct foo { foo()

  • 0

Background

Consider the following code:

#include <utility>

namespace ns
{
    struct foo
    {
        foo() : i(0) {}
        int i;
        
    private:
        foo(const foo&); // not defined,
        foo& operator=(const foo&); // non-copyable
    };
    
    void swap(foo& lhs, foo& rhs)
    {
        std::swap(lhs.i, rhs.i);
    }
}

template <typename T>
void do_swap(T& lhs, T& rhs); // implementation to be determined

int main()
{
    ns::foo a, b;
    do_swap(a, b);
}

In C++03, this implementation of do_swap would be considered "broken":

template <typename T>
void do_swap(T& lhs, T& rhs)
{
    std::swap(lhs, rhs);
}

By explicitly specifying std::, it prohibits ns::swap from being found via argument-dependent lookup. (It then fails to compile because std::swap tries to copy a foo, which is not allowed.) Instead, we do this:

template <typename T>
void do_swap(T& lhs, T& rhs)
{
    using std::swap; // allow std::swap as a backup if ADL fails to find a swap
    swap(lhs, rhs); // unqualified call to swap, allow ADL to operate
}

Now ns::swap is found and std::swap, being less specialized, is not used. It’s uglier, but it works and is understandable in hind-sight. boost::swap wraps this up nicely for us (and provides array overloads):

#include <boost/swap.hpp>

template <typename T>
void do_swap(T& lhs, T& rhs)
{
    boost::swap(lhs, rhs); // internally does what do_swap did above
}

Question

Does std::swap take on the behavior of boost::swap in C++11? If not, why?

To me it seems obvious that it ought to. Any code broken by the change was probably quite flimsy in the first place (algorithms and containers, like std::sort and std::vector, were underspecified; implementations were allowed to call ADL swap’s or not indeterminately), so the change would be for the better. Additionally, std::swap is now defined for arrays, so change at all certainly isn’t out of the question.

However, while §17.6.3.2 specifies that all calls to swap within the standard library must be done without std:: qualification (fixing the problem with algorithms and containers noted above), it fails to touch on std::swap itself. It even gives examples of swapping values that include using std::swap;. Likewise §20.2.2 (where std::swap is specified) doesn’t say a word on ADL.

Lastly, GCC does not enable ADL in their std::swap implementation (nor does MSVC, but that’s not saying much). So I must be wrong that std::swap takes on the behavior of boost::swap, but I don’t understand why the change wasn’t made. 🙁 And I’m not alone!

  • 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-30T01:27:57+00:00Added an answer on May 30, 2026 at 1:27 am

    I would have had to vote against your proof-of-concept implementation had it been proposed. I fear it would break the following code, which I’m pretty sure I’ve seen in the wild at least once or twice over the past dozen years.

    namespace oops
    {
    
        struct foo
        {
            foo() : i(0) {}
            int i;
    
            void swap(foo& x) {std::swap(*this, x);}
        };
    
        void swap(foo& lhs, foo& rhs)
        {
            lhs.swap(rhs);
        }
    
    }
    

    Whether you think the above is good code or bad, it works as the author intends in C++98/03 and so the bar for silently breaking it is pretty high. Telling users that in C++11 they would no longer have to write using std::swap; isn’t a sufficiently high benefit to outweigh the disadvantage of silently turning the above code into infinite recursion.

    Another way to get out of writing using std::swap; is to use std::iter_swap instead:

    template <typename T>
    void do_swap(T& lhs, T& rhs)
    {
        std::iter_swap(&lhs, &rhs); // internally does what do_swap did above
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: class Foo { // boring parts omitted private TcpClient socket;
Background Consider the following: template <unsigned N> struct Fibonacci { enum { value =
Background Consider the following input: <Foo Bar=bar Baz=1 Bax=bax > After processing, I need
Consider the following code, which takes place in a background thread (thread B): List<T>
Background Distinguish between model values and predicted values. Problem Consider the following code: library(
Consider the following code: [context performBlock:^{ // add a bunch of objects to context
Consider the following code. <ul> <li><a href=#1 >Item 1</a></li> <li><a href=#2 >Item 2</a></li> <li><a
Background To replace invalid zip codes. Sample Data Consider the following data set: Typo
Consider the following JavaScript: for (var i = 0; i < foo.length; i++) {
Please consider the following code: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <html

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.