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
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
  • 13,582
  • 19
  • 75
  • 132
9
votes
1 answer

ambiguous overload for ‘operator=’ with c++11 std::move and copy and swap idiom

I am getting the following error: [matt ~] g++ -std=c++11 main.cpp -DCOPY_AND_SWAP && ./a.out main.cpp: In function ‘int main(int, const char* const*)’: main.cpp:101:24: error: ambiguous overload for ‘operator=’ in ‘move = std::move((* &…
7
votes
3 answers

Does it make sense to use the move-and-swap idiom on a movable and non-copyable class

If I have a class such as class Foo{ public: Foo(){...} Foo(Foo && rhs){...} operator=(Foo rhs){ swap(*this, rhs);} void swap(Foo &rhs); private: Foo(const Foo&); // snip: swap code }; void swap(Foo& lhs, Foo& rhs); Does it make…
Flame
  • 2,166
  • 2
  • 20
  • 40
7
votes
4 answers

copy and swap idiom with pure virtual class

I am trying to implement virtual class with pure virtual method and 'copy and swap' idiom, but I've encountered some problems. Code won't compile because I am creating instance in the assign operator of the class A which contains pure virtual…
Rob
  • 708
  • 8
  • 27
6
votes
1 answer

Assignment via copy-and-swap vs two locks

Borrowing Howard Hinnant's example and modifying it to use copy-and-swap, is this op= thread-safe? struct A { A() = default; A(A const &x); // Assume implements correct locking and copying. A& operator=(A x) { std::lock_guard
Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
6
votes
5 answers

Why SGI STL don't use the copy-and-swap idiom?

I recently read an answer on StackOverflow about What is the copy-and-swap idiom? and knew that the copy-and-swap idiom can avoiding code duplication, and providing a strong exception guarantee. However, when I looked into SGI STL deque…
abcdabcd987
  • 2,043
  • 2
  • 23
  • 34
6
votes
2 answers

What is the proper approach to swap and copy idiom in virtual inheritance?

Consider classic virtual inheritance diamond hierarchy. I wonder to know what is the right implementation of copy and swap idiom in such hierarchy. The example is a little artificial - and it is not very smart - as it would play good with default…
5
votes
1 answer

How do I swap an MFC CString?

OK, so I'm all sold on the copy-and-swap idiom and I think I mostly know how to implement it. However, or codebase uses MFC's CString class as string and this ain't gonna change. Since swap must (should???) be nothrow, I cannot…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
5
votes
1 answer

linking error in friend function of a template class

I have template class node and in order to follow the copy-and-swap idiom i am tryig to write the swap function which is a friend of class Node my code is : Node.h #ifndef NODE_H #define NODE_H #include template class Node { …
Hummingbird
  • 647
  • 8
  • 27
4
votes
3 answers

Is it OK to have a throwing swap member-implementation?

The general guideline when writing classes (using the copy-and-swap idiom) is to provide a non throwing swap member function. (Effective C++, 3rd edition, Item 25 and other resources) However, what if I cannot provide the nothrow guarantee because…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
4
votes
5 answers

Implementing the swap in the copy and swap idiom

Following What is the copy and swap idiom and How to provide a swap function for my class, I tried implementing the swap function like in the latter accepted answer option number 2 (having a free function that calls a member function) instead of the…
green diod
  • 1,399
  • 3
  • 14
  • 29
2
votes
2 answers

Copy-and-swap done through interfaces

I'm trying to implement a copy+swap idiom to achieve strong-exception safety through a level of abstraction and, although the principle is clear, as it's often the case the devil is in the detail. Say I have a class that looks like this: class…
2
votes
2 answers

Lock-free Shared Memory in C++ for Variable length Records

I am newbee to IPC. The Writer process writes the data into shared memory, Many reader processes reads the data. The data to be written have a unique identifier, has to be be indexed by unique key for the faster access( like STL::map or hashmap for…
naveenhegde
  • 177
  • 1
  • 1
  • 7
2
votes
2 answers

Copy-and-Swap idiom for class with references to abstract classes

I'm trying to implement the Copy-and-Swap Idiom for my class, because I need to implement operator=, and since it has reference members, and references can only be assigned once, I thought that the aforementioned idiom was a valid workaround. But…
dario_ramos
  • 7,118
  • 9
  • 61
  • 108
2
votes
2 answers

Unresolved external using template class with copy-and-swap

I'm getting a linker error when using a template class where I tried implementing the copy-and-swap idiom as suggested here: What is the copy-and-swap idiom? The template class, let's call it "TemplateClass" is partially defined like this: template<…
Tim Meyer
  • 12,210
  • 8
  • 64
  • 97