
C/C++
Object Swapping Part 1: Its Surprising Importance
By Andrew Koenig, March 14, 2012
I am beginning to believe that swapping is as fundamental an operation as copying, and that it may even be more fundamental than assignment.Related Reading
More Insights
White Papers
- 2021 Banking and Financial Services Industry Cyber Threat Landscape Report
- The Rise of Extended Detection & Response
Reports
- AI-Driven Testing: Bridging the Software Automation Gap
- Enterprise Cybersecurity Plans in a Post-Pandemic World
Webcasts
- Cyber Resiliency 2023: How to Keep IT Operations Running, No Matter What
- Demystifying IT Automation Virtual Event 4/6

Currently we allow the following HTML tags in comments:
Single tags
These tags can be used alone and don't need an ending tag.
<br>
Defines a single line break
<hr>
Defines a horizontal line
Matching tags
These require an ending tag - e.g. <i>italic text</i>
<a>
Defines an anchor
<b>
Defines bold text
<big>
Defines big text
<blockquote>
Defines a long quotation
<caption>
Defines a table caption
<cite>
Defines a citation
<code>
Defines computer code text
<em>
Defines emphasized text
<fieldset>
Defines a border around elements in a form
<h1>
This is heading 1
<h2>
This is heading 2
<h3>
This is heading 3
<h4>
This is heading 4
<h5>
This is heading 5
<h6>
This is heading 6
<i>
Defines italic text
<p>
Defines a paragraph
<pre>
Defines preformatted text
<q>
Defines a short quotation
<samp>
Defines sample computer code text
<small>
Defines small text
<span>
Defines a section in a document
<s>
Defines strikethrough text
<strike>
Defines strikethrough text
<strong>
Defines strong text
<sub>
Defines subscripted text
<sup>
Defines superscripted text
<u>
Defines underlined text
Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.
![]() |
To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy. |
Hello,
Either I missed something or it is the well known copy&swap idiom (http://en.wikibooks.org/wik...
This is old news, but nevertheless still doesn't get the attention it should in modern C++ design, so thanks for the refresher.
With C++11 there's a new reason to make "swap" a first-class citizen next to constructors, destructors and assignment operator: "Unifying assgiment"
http://en.wikibooks.org/wik...
For reference Herb Sutter on implementing assigment in terms of "swap" and it's benefit for exception safety:
http://www.gotw.ca/gotw/059...
It's an interesting idea and a common approach to implement a new component by using existing components; but in this case, why not just treat copy, assignment, and swap as three basic object operations on their own?
This is good food for thought. You've made a good argument for exception handling.
I don't quite see this as a general purpose solution however.
I'll be interested to see how this could improve performance. I can imagine scenarios where this could temporarily improve performance, by deferring the destruction of child objects and the freeing of resources until after the meaty work is done.
For that you'd likely want most of your data laden members to be pointers, so that they can be swapped between objects with minimal overhead.
I'm drawing a blank on other scenarios that would benefit, as you seem to be increasing the amount of work done in an assignment, without eliminating any pre-existing steps. You've just changed the order.