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
2
votes
0 answers

Why is std::swap preferred over std::move when dealing with temporaries?

Reading through the reference of Copy assignment operator, i noticed that it suggests copy-and-swap idiom when it comes to the first option of the operator signature: class-name & class-name :: operator= ( class-name ) And later on the same page…
The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
2
votes
0 answers

Copy-Assignment Operator of class uses (copy and swap) and ordinary implement give different result

#include #include #include #include #include #include using std::cout; using std::vector; class Has_ptr { public: friend void swap(Has_ptr &lhs, Has_ptr &rhs); friend bool…
David Zhou
  • 21
  • 1
  • 2
2
votes
1 answer

Pass by value vs. pass by reference in copy assignment operator

First and foremost, there's a similar popular post What is the copy-and-swap idiom?. The accepted answer has a link to https://web.archive.org/web/20140113221447/http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/. Both the accepted and…
24n8
  • 1,898
  • 1
  • 12
  • 25
2
votes
2 answers

error: ambiguous overload for 'operator=' in swap function using the copy-and-swap idiom

While using the copy-and-swap idiom in a class that has constant references as members, the above error occurs. Example code: #include #include using std::reference_wrapper; class I_hold_reference; void…
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
2
votes
2 answers

What does `std::swap` do when applied to these objects?

The code using namespace std; class A { private: vector a; public: A(vector x):a(x){} string toString() { string s; for (auto& element : a) { s += to_string(element) + " "; } return s; …
Remi.b
  • 17,389
  • 28
  • 87
  • 168
2
votes
1 answer

copy-swap idom : what if I change the class members?

I have a class class MyClass { public : int a; int b; } For using the copy-swap idiom I then create the function void MyClass::swap(MyClass& other) { std::swap(a,other.a); std::swap(b,other.b); } If, later, I change my class and remove the…
Laurent Claessens
  • 547
  • 1
  • 3
  • 18
1
vote
1 answer

Trying to use copy and swap idiom on operator=

While trying to implement MyVector I end up with: #include #include using namespace std; template class MyVector { int m_size = 0; int m_capacity = 0; T* m_data = nullptr; public: MyVector() …
KcFnMi
  • 5,516
  • 10
  • 62
  • 136
1
vote
0 answers

Why copy constructor need assignment operator?

Being beginner in C++, I have found following code while reading on copy constructor and assignment operator. Right below the code, I found it needs operator=. I don't understand why there is rule of three, just by having copy constructor can't this…
1
vote
1 answer

swapList() and operator=() functions in a LinkedList class keep crashing in C++

I need a swapList(LinkedList& Other) function in charge of basically swapping the values of two lists. Currently, it takes the last element of Other and inputs it into the first element of *this. Then it also moves the last element of Other to the…
Ed2Cute
  • 11
  • 5
1
vote
2 answers

Why does copy-and-swap in a base class cause the copy-assignment operator to be implicitly deleted in the derived class?

Tested only in GCC and Clang, the presence of a pass-by-value copy assignment operator in the base class (useful when implementing the copy-and-swap (or copy-and-move) idiom) causes the copy assignment operator in the derived class to be implicitly…
1
vote
1 answer

why isn't my copy constructor called when I do a copy and swap idiom?

In the following code, when assignment operator is used, why is the copy constructor not being called or why is there no print corresponding to it? #include #include using std::cout; using std::endl; class Person { private: …
1
vote
1 answer

Copy&Swap efficiency for shared pointers

This can be seen as a follow up to e.g. Why shared pointer assignment does 'swap'?. The question is about the Copy&Swap idiom used e.g. in boost. I do understand that the benefit of Copy&Swap is to reuse existing code which avoids duplication and…
Flamefire
  • 5,313
  • 3
  • 35
  • 70
1
vote
1 answer

Move semantics for a plurality of constructor parameters

An object's constructor requires twp arguments from the user. It is required that the parameters may be of different types, but each type may be assumed to implement proper move-semantics. (The actual example is a cubic_spline class, that is…
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
1
vote
2 answers

Reset an object

Lately I often reset an object by assigning a new value to it with operator=. Most of my classes have a copy constructor and operator= defined using "copy and swap" idiom. Which works fine in most cases, albeit not as efficient as it could be, but…
rozina
  • 4,120
  • 27
  • 49
1
vote
4 answers

Providing swap() for a C++ template class breaks std::swap()?

I was trying to implement the copy-and-swap idiom in my custom Matrix class, and I ran into some trouble with the implementation of swap() in the way suggested in the linked-to question: (The compiler I used is the one from MS VS2010 IDE, dialect is…