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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:52:54+00:00 2026-06-08T09:52:54+00:00

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

  • 0

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

Every time I think I understand it, I see something that really confuses me.

If you want to provide an implementation of swap for your own class, what do you do?

The list of possibilities is:

  1. Define one inside the std namespace (taking two arguments), which calls #3 below
    (some say this is correct; some people say illegal)

  2. Define a static method inside the class, taking two arguments to swap
    (makes more sense to me than #4, but I don’t get why no one does this), which calls any base-class swaps as necessary

  3. Define an instance method inside the class, taking one other argument to swap with, which calls any base-class swaps as necessary

  4. Define an instance method inside the class, taking two other arguments to swap, also here, which calls any base-class swaps as necessary

  5. Define one in your own namespace (taking two arguments), which calls #3

  6. Something else

My own understanding is that I need #5 and #3, where the caller would then be calling swap likeusing std::swap; swap(a, b);,but the fact that no one seems to suggest that combination is really confusing me. And I really don’t understand #4 at all, because everyone seems to be using an instance member when in fact the operation is static. I can’t tell if my understanding is wrong or a bunch of the answers I see when looking this up.

What’s the correct way?

  • 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-08T09:52:56+00:00Added an answer on June 8, 2026 at 9:52 am

    A common pattern I have seen is providing 3 and 5, as per your own understanding.

    1. adds an specialization to the std:: namespace, which is allowed, but might not be possible in all cases (if your type is a template itself).
    2. offers no advantage at all, and forces qualifying with the type when used outside of one of the members, which means that for implementing swap on other types that hold your type as a member, they will need to qualify the call (void swap( other& l, other& r ) { T::swap( l.t, r.t ); })
    3. does not need friendship, allows for use with rvalues (even in C++03) and is idiomatic in some cases std::vector<int>().swap( v ); to clear the contents of the vector.
    4. What? You misunderstood the code! That is not declaring a member taking two arguments, but rather a free function taking the two arguments, and defines the function inline. This is equivalent to 5 (without forwarding to 3, but rather implementing everything in the free function).
    5. Free function in the same namespace allows for ADL to find it, and enables other code to use the common pattern of void swap( other& l, other& r ) { using std::swap; swap( l.t, r.t ); } without having to know whether the type of other::t has an specific swap overload or the one in std:: needs to be used. Forwarding to 3 allows you to provide a single (real) implementation that can be used through ADL and also on temporary objects.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how to provide a swap function for my class? There are some
Possible Duplicate: C++ Timer function to provide time in nano seconds High resolution timer
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: What is a CAPTCHA that is compatible with ASP.NET MVC ? Please
Possible Duplicate: Merging: hg/git vs. svn Hi, I'm a long time SVN user and
Possible Duplicate: Best way to provide charts on the web? Server-side or client-side library?
Possible Duplicate: Why Option[T]? Reference types provide the special value null meaning "absence of
Possible Duplicate: What is “loose coupling?” Please provide examples. What makes coupling loose and
Possible Duplicate: How can I determine a web user's time zone? Which is the

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.