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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:08:54+00:00 2026-05-23T09:08:54+00:00

Possible Duplicate: how to provide a swap function for my class? There are some

  • 0

Possible Duplicate:
how to provide a swap function for my class?

There are some questions about this, but a lot of contradictions (person A giving solution A’ with many upvotes with person B saying it’s UB) or “only works if the compiler supports ADL” is answered.

So, say I have the following template (container) class:

template<typename T>
class C {
    // ...
    void swap(C<T>& y) throw(); // C x; x.swap(y);
}

then what is the correct way to make sure this (example) code works:

C<int> x, y;
std::swap(x, y);

Please give your answer for C++03, and if it still works in C++0x, even better!

  • 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-23T09:08:54+00:00Added an answer on May 23, 2026 at 9:08 am

    You are not allowed to overload functions in the std-namespace.

    Declare swap as a free function, overloaded in the same namespace as your class C:

     template<class T>
     void swap(C<T>& x, C<T>& y) { x.swap(y); }
    

    The right way to swap is to import std::swap and use a non-qualified version (which is retreieved via namespace-based Koenig lookup):

     template<class T>
     void dostuff(T x, T y) {
        ...
        using std::swap;
        swap(x,y);
        ...
     }
    

    That will use C::swap if x and are C, and std::swap for types that do not have their own swap.

    (The import of std::swap like above is only necessary in template functions where the type is not known. If you know you have a C, then you can use x.swap(y) right away w/o problems.)

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

Sidebar

Related Questions

Possible Duplicate: Should we support IE6 anymore? There is a lot of talk about
Possible Duplicate: C++ Timer function to provide time in nano seconds High resolution timer
Possible Duplicate: What are Extension Methods? I know this questions has been asked previously,
Possible Duplicate: Can jQuery provide the tag name? Hi! This question is so basic
Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Close and Dispose - which to call? Hi, After reading some web
Possible Duplicate: What to do when I want to use database constraints but only
Possible Duplicate: Plain English explanation of Big O I was recently asked about my

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.