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.

58 questions
2138
votes
5 answers

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Does the idiom change when C++11 is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is…
GManNickG
  • 459,504
  • 50
  • 465
  • 534
184
votes
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; …
towi
  • 20,210
  • 25
  • 94
  • 167
65
votes
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
  • 1,940
  • 1
  • 21
  • 40
31
votes
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,116
  • 1
  • 24
  • 35
29
votes
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
  • 234,851
  • 74
  • 306
  • 532
28
votes
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…
cdmh
  • 3,167
  • 1
  • 21
  • 40
25
votes
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,274
  • 1
  • 32
  • 60
25
votes
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…
Aberrant
  • 3,283
  • 1
  • 24
  • 37
19
votes
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…
lizarisk
  • 6,692
  • 9
  • 42
  • 66
17
votes
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&…
Walter
  • 40,885
  • 16
  • 97
  • 176
16
votes
2 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…
13
votes
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…
Darius
  • 583
  • 4
  • 15
11
votes
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); …
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
10
votes
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…
Mr.C64
  • 37,988
  • 11
  • 76
  • 141
9
votes
3 answers

Safe assignment and copy-and-swap idiom

I'm learning c++ and I recently learned (here in stack overflow) about the copy-and-swap idiom and I have a few questions about it. So, suppose I have the following class using a copy-and-swap idiom, just for example: class Foo { private: int *…
Rafael S. Calsaverini
  • 12,352
  • 16
  • 69
  • 126
1
2 3 4