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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:04:10+00:00 2026-05-24T01:04:10+00:00

I was thinking about the swap() idiom. If we have a valid copy constructor,

  • 0

I was thinking about the swap() idiom.

If we have a valid copy constructor, why cant the compiler generate a “swap” function behind the scene, by swapping the “this” pointers?

I am probably looking at this too simplistically, but i’m wondering why it cant be done.

  • 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-24T01:04:11+00:00Added an answer on May 24, 2026 at 1:04 am

    I suppose it could, but it doesn’t. The default versions of the constructors and assignments just construct/assign member-by-member. If you already need to write your own copy constructor (which apparently is a warning sign and should only happen in rare cases), your class has something non-trivial going on, and that most likely won’t do the right thing.

    Also, if the copy-constructor has to be written by hand and isn’t just copying of members, then chances are that the swap function is non-trivial, too, so you’d have to provide that manually as well.

    In C++0x, you can use = default to make the compiler generate constructors automatically and assignment operators automatically, but those will just operate member-by-member and not use the copy+swap idiom.

    For most classes, however, you should be using resource managing containers for your members, which themselves implement the all the correct functionality, and your class itself should be able to use the default copy/move constructors and assignments.

    Edit. To put it another way: Copy/Move+Swap is an idiom that you may choose, but it isn’t the default way to recursively build larger types from smaller ones. There is no automatically implied swap (here is a previous discussion on the matter) because it’s left to the user’s design choices which way round to implement copy, move and swap.

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

Sidebar

Related Questions

I've recently come to appreciate the copy-and-swap idiom, and have been using it to
For about a year I have been thinking about writing a program that writes
I was thinking about doing a take_timing function that would take the timing of
I could not sleep last night and started thinking about std::swap . Here is
I´m thinking about to write an application will have to store a small amount
While thinking about the way to implement Scheme R5RS, I have become puzzled about
I have been thinking about adding a button to a View which when pressed
I am currently thinking about some efficiency/speed issues. I have about 40 a elements
I was thinking about integrating some instant messaging function into an existing ASP.NET web
I'm thinking about the tokenizer here. Each token calls a different function inside 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.