copy-and-swap Questions

3

Solved

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 th...
Dermatoglyphics asked 18/8, 2017 at 10:17

4

Solved

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 operat...
Lector asked 6/5, 2014 at 14:7

0

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){ ...
Byssus asked 22/7, 2018 at 8:35

2

Solved

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; } Howe...
Ferdy asked 12/4, 2017 at 11:35

3

Solved

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 Unmova...

4

Solved

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...
Cashmere asked 14/7, 2011 at 1:3

2

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 e...
Nicolas asked 20/11, 2015 at 23:1

2

Solved

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...
Stenophyllous asked 16/11, 2015 at 11:59

1

Solved

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=(...
Buttery asked 26/8, 2015 at 18:58

5

Solved

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...
Insightful asked 22/5, 2015 at 13:19

2

Solved

In What is the copy-and-swap idiom this example is shown: friend void swap(dumb_array& first, dumb_array& second) // nothrow { // enable ADL (not necessary in our case, but good practice)...
Stuartstub asked 24/1, 2015 at 21:52

5

Solved

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 construct...

3

Solved

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" operat...
Anthropophagi asked 7/11, 2013 at 16:41

2

Solved

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...
Laquitalar asked 17/9, 2013 at 11:7

1

Solved

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) ...
Floorage asked 18/8, 2013 at 20:22

3

Solved

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 i...
Oratorical asked 3/3, 2013 at 17:3

1

Solved

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 o...

2

Solved

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 ...

1

Solved

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 ...
Niu asked 5/10, 2011 at 12:1

6

Solved

I'm trying to put the copy-and-swap idiom into a reusable mixin: template<typename Derived> struct copy_and_swap { Derived& operator=(Derived copy) { Derived* derived = static_cast&lt...
Madcap asked 16/8, 2011 at 14:48

3

Solved

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: swa...

3

Solved

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-swa...
Sentience asked 6/5, 2011 at 0:25

2

Solved

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...
Berck asked 17/4, 2011 at 18:35

1

Solved

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. ...
Slightly asked 21/2, 2011 at 22:36

2

Solved

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 ...
Volley asked 27/1, 2010 at 0:37

© 2022 - 2024 — McMap. All rights reserved.