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
0
votes
1 answer

The copy-and-swap idiom in C++20

I found this great answer from 12 years ago that explains the copy-and-swap idiom and how to implement it in C++11 and C++03. My question is: is this still the recommended solution or has it changed in later versions of C++? (specifically I'm…
traveh
  • 2,700
  • 3
  • 27
  • 44
0
votes
0 answers

Implement C++ assignment operator in terms of constructor

Background Suppose you want to implement a resource-managing class in C++. You cannot use the Rule of Zero or Rule of Five Defaults, so you actually need to implement copy and move constructor, copy and move assignment operator, and destructor. In…
Daniel H
  • 7,223
  • 2
  • 26
  • 41
0
votes
1 answer

Do I understand RAII in conjunction with the copy/swap idiom correctly?

class Resource { Handle resource_handle; public: friend void swap(Resource &a, Resource &b); // swap for the partial copy/swap idiom Resource(); // Default with uninitialized handle whose destruction is a noop Resource(std::string…
salbeira
  • 2,375
  • 5
  • 26
  • 40
0
votes
1 answer

swap elements in an array causing issue

Happy New Year. I have a question about swapping elements in an array under Python environment. The code w/o issues are: def findMissingNumber(nums): length = len(nums) result = [] i = 0 while i < length: # num = nums[i] …
Qiang Super
  • 323
  • 1
  • 11
0
votes
0 answers

Copy-swap idiom not recommended?

For a long time time I though that the correct way to implement a copy assignment (for a non trivial class) was to use the copy-swap idiom. struct A{ ... pointer to data A(A const& other){ ... complicated stuff, allocation, loops, etc …
alfC
  • 14,261
  • 4
  • 67
  • 118
0
votes
1 answer

How to correctly make deep copy for abstract classes?

I'm working on a collision system for which I need to copy the colliders of entities. I make the system such as I don't have to set in stone how I want to handle collision, (and I will start using AABB likely, but might change to SAT) but I will…
Pierre-Antoine Guillaume
  • 1,047
  • 1
  • 12
  • 28
0
votes
3 answers

Why do we use copy and swap when using assignment operator?

I have a question about assignment operator when using copy-and-swap method. String & operator = (String s) // the pass-by-value parameter serves as a temporary { s.swap (*this); // Non-throwing swap return *this; }// Old resources released…
0
votes
0 answers

Why do I need the copy-and-swap idiom?

I can't see any point to using the copy-and-swap idiom in C++11. If I need a handle, I can use unique_ptr or shared_ptr. If I need a collection of objects, I can just vector or string. struct Relax { shared_ptr resource; public: /*…
0
votes
1 answer

Swap-Idiom of abstract class with private data members

Assume we have this two classes, with implemented swap-idioms. The copy constructor and assignment operator of the base class are deleted, as it makes no sense. However the swap-method is implemented, as it holds a member. namespace std { …
user5294035
0
votes
1 answer

When to use std::swap for stream types?

Trying to give an answer to this question text-file-handling-in-c giving references to cplusplus.com. I came across to the std::swap-function for stream-types like fstream. So my question is: What exactly is the purpose of a swap functionality e.g.…
mbed_dev
  • 1,450
  • 16
  • 33
0
votes
1 answer

Using the copy-and-swap idiom, how does the destructor of the copied object not deallocate pointed to memory?

I was reading the following question: What is the copy-and-swap idiom? I was under the impression that when an object is passed by value, it's pointers and values are copied, but the memory pointed to by the passed object's pointers is not copied. …
0
votes
4 answers

How would move semantics improve "my way"?

Background I read the following answers earlier today, and it felt like relearning C++, litterally. What are move semantics? What is the copy-and-swap idiom? Then I wondered if I should change my "ways" to use these exciting features; the main…
Jonathan H
  • 7,591
  • 5
  • 47
  • 80
0
votes
1 answer

Does copy-and-swap still give the strong exception guarantee in C++11?

The copy-and-swap idiom is said to provide a strong exception guarantee. But in C++11, std::swap uses move operations. Consider the following code: class MyClass { AClass x; CanThrowIfMoved throwingObject; MyClass(MyClass&& other)…
Aberrant
  • 3,423
  • 1
  • 27
  • 38
0
votes
1 answer

how to swap elements in a multidimensional array in php

I'm working on arrays using php and I am trying to swap items of the array. I have this code:

user1998735
  • 243
  • 1
  • 3
  • 13
0
votes
4 answers

Utilizing Copy constructor for =overloading

I have a class with two vectors: an int and an Str. Now I want to define a copy constructor so that the order of elements is reversed; e.g. if a=(1,Hello),(2,World) and I write auto b=a; I get b=(2,world),(1,hello). Which is working perfectly. The…