copy-and-swap

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

删除回忆录丶 提交于 2021-02-08 14:10:40
问题 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 implementation, I found that it doesn't use the idiom. I'm wondering why not, if the idiom is somehow like a "best practice"? deque& operator= (const deque& __x) { const size_type __len = size(); if (&__x != this) { if (__len >= __x.size()) erase(copy(__x

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

落花浮王杯 提交于 2020-05-12 21:20:05
问题 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 sense to implement operator= by value and swap if I don't have a copy constructor? It should prevent copying my objects of class Foo but allow moves. This class is not copyable so I shouldn't be able to copy construct or copy assign it. Edit I've tested my code with

How to use noexcept in assignment operator with copy-and-swap idiom?

拟墨画扇 提交于 2019-12-30 00:55:15
问题 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 specifier in this case? The copy construction can throw, but I doubt whether it can violate the noexcept specifier. // Is it correct considering that T copy constructor can throw? T& operator=(T other) noexcept; 回答1: Since the copy is made on the

Copy-swap idiom not recommended?

陌路散爱 提交于 2019-12-22 00:23:37
问题 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 } void swap(A& other){... cheap stuff ...} A& operator=(A const& other){ A tmp{other}; swap(other); return *this; } }; But then I heard this talk https://www.youtube.com/watch?v=vLinb2fgkHk by Howard Hinnant where he says that the copy-swap idiom is good to

Move Assignment incompatible with Standard Copy and Swap

心不动则不痛 提交于 2019-12-21 03:12:18
问题 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 use the standard "Copy and Swap" idiom. This is the class I am using: #include <string.h> #include <utility> class String { int len; char* data; public: // Default constructor // In Terms of C-String constructor String() : String("") {} // Normal

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

妖精的绣舞 提交于 2019-12-07 07:32:41
问题 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 copy semantic for A,B,D classes. But just to illustrate the problem - please forget about the example weaknesses and provide the solution. So I have class D derived from 2 base classes (B<1>,B<2>) - each of B classes inherits virtually from A class.

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

瘦欲@ 提交于 2019-12-05 16:26:49
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 copy semantic for A,B,D classes. But just to illustrate the problem - please forget about the example weaknesses and provide the solution. So I have class D derived from 2 base classes (B<1>,B<2>) - each of B classes inherits virtually from A class. Each class has non trivial copy semantics with using of copy and swap idiom. The most derived D class

Assignment via copy-and-swap vs two locks

随声附和 提交于 2019-12-01 05:06:13
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<std::mutex> lock_data (_mut); using std::swap; swap(_data, x._data); return *this; } private: mutable std::mutex _mut; std::vector<double> _data; }; I believe this thread-safe (remember op='s parameter is passed by value), and the only problem I can find is the one swept under the rug: the copy ctor. However, it would be a rare class that allows copy-assignment but not

Assignment via copy-and-swap vs two locks

泪湿孤枕 提交于 2019-12-01 02:19:24
问题 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<std::mutex> lock_data (_mut); using std::swap; swap(_data, x._data); return *this; } private: mutable std::mutex _mut; std::vector<double> _data; }; I believe this thread-safe (remember op='s parameter is passed by value), and the only problem I can find is the one swept

What is the Rule of Four (and a half)?

ε祈祈猫儿з 提交于 2019-11-30 06:54:07
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 Four (and a half) , which is a combination of the Rule of Five and the copy-and-swap idiom. So what exactly is the Rule of Four (and a half)? Which functions need to be implemented, and what should each function's body look like? Which function is the half? Are there any disadvantages or warnings for this approach, compared to the Rule of Five?