Questions tagged [copy-and-swap]

The copy swap idiom in C++ can be used to simplify the implementation of the assignment operator by used the copy constructor to generate a local copy and swapping it with the current object.

The copy swap idiom in C++ can be used to simplify the implementation of the assignment operator by used the copy constructor to generate a local copy and swapping it with the current object.

Use this tag for questions related to the copy and swap idiom.

For more detail, see also this Stackoverflow question.

66 questions
5 answers

What is the copy-and-swap idiom?

What is the copy-and-swap idiom and when should it be used? What problems does it solve? Does it change for C++11? Related: What are your favorite C++ Coding Style idioms: Copy-swap Copy constructor and = operator overload in C++: is a common…
  • 494,350
  • 52
  • 494
  • 543
2 answers

public friend swap member function

In the beautiful answer to the copy-and-swap-idiom there is a piece of code I need a bit of help: class dumb_array { public: // ... friend void swap(dumb_array& first, dumb_array& second) // nothrow { using std::swap; …
  • 21,587
  • 28
  • 106
  • 187
4 answers

Why do some people use swap for move assignments?

For example, stdlibc++ has the following: unique_lock& operator=(unique_lock&& __u) { if(_M_owns) unlock(); unique_lock(std::move(__u)).swap(*this); __u._M_device = 0; __u._M_owns = false; return *this; } Why not just…
Display Name
  • 2,323
  • 1
  • 26
  • 45
2 answers

What is copy elision and how does it optimize the copy-and-swap idiom?

I was reading Copy and Swap. I tried reading some links on Copy Elision but could not figure out properly what it meant. Can somebody please explain what this optimization is, and especially what is mean by the following text This is not just a…
Yogesh Arora
  • 2,216
  • 2
  • 25
  • 35
3 answers

Move Assignment incompatible with Standard Copy and Swap

Testing out the new Move Semantics. I just asked about an issues I was having with the Move Constructor. But as it turns out in the comments the problem is really that the "Move Assignment" operator and "Standard Assignment" operator clash when you…
Martin York
  • 257,169
  • 86
  • 333
  • 562
1 answer

When is overloading pass by reference (l-value and r-value) preferred to pass-by-value?

I have seen it said that a operator= written to take a parameter of the same type by-value serves as both copy assignment operator and move assignment operator in C++11: Foo& operator=(Foo f) { swap(f); return *this; } Where the alternative…
  • 3,294
  • 2
  • 26
  • 41
2 answers

Copy & Move Idiom?

By using the Copy & Swap idiom we can easily implement copy assignment with strong exception safety: T& operator = (T other){ using std::swap; swap(*this, other); return *this; } However this requires T to be Swappable. Which a type…
Emily L.
  • 5,673
  • 2
  • 40
  • 60
3 answers

What is the Rule of Four (and a half)?

For properly handling object copying, the rule of thumb is the Rule of Three. With C++11, move semantics are a thing, so instead it's the Rule of Five. However, in discussions around here and on the internet, I've also seen references to the Rule of…
3 answers

Should the Copy-and-Swap Idiom become the Copy-and-Move Idiom in C++11?

As explained in this answer, the copy-and-swap idiom is implemented as follows: class MyClass { private: BigClass data; UnmovableClass *dataPtr; public: MyClass() : data(), dataPtr(new UnmovableClass) { } MyClass(const…
  • 3,423
  • 1
  • 27
  • 38
2 answers

How to use noexcept in assignment operator with copy-and-swap idiom?

The move assignment operator should often be declared noexcept (i.e. to store the type in STL containers). But the copy-and-swap idiom allows both copy- and move- assignment operators to be defined in a single piece of code. What to do with noexcept…
  • 7,562
  • 10
  • 46
  • 70
2 answers

Why does std::vector have two assignment operators?

Since 2011, we have both copy and move assignment. However, this answer argues quite convincingly that, for resource managing classes, one needs only one assignment operator. For std::vector, for example, this would look like vector&…
  • 44,150
  • 20
  • 113
  • 196
2 answers

using swap to implement move assignment

Something occurred to me which I think it completely reasonable, but I'd like people's opinion on it in case I'm just completely missing something. So firstly, my understanding of T& operator=(T&& rhs) is that we don't care what the contents of rhs…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
2 answers

When is copy-and-swap idiom not applicable

After reading this about the copy-and-swap idiom I've read this which says under (2): class_name & class_name :: operator= ( const class_name & ) (2) (2) Typical declaration of a copy assignment operator when copy-and-swap idiom cannot…
  • 707
  • 6
  • 21
6 answers

reusing the copy-and-swap idiom

I'm trying to put the copy-and-swap idiom into a reusable mixin: template struct copy_and_swap { Derived& operator=(Derived copy) { Derived* derived = static_cast(this); derived->swap(copy); …
  • 256,549
  • 94
  • 388
  • 662
3 answers

Inefficiency of copy-and-swap idiom?

I was testing some code where there is a std::vector data member inside a class. The class is both copiable and movable, and the operator= is implemented as described here using the copy-and-swap idiom. If there are two vectors, say v1 with big…
  • 41,637
  • 14
  • 86
  • 162
2 3 4 5