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 discussi
So what exactly is the Rule of Four (and a half)?
“The Rule of The Big Four (and a half)" states that if you implement one of
then you must have a policy about the others.
Which functions need to implemented, and what should each function's body look like?
move constructor (using default constructor and swap) :
S(S&& s) : S{} { swap(*this, s); }
assignment operator (using constructor and swap)
S& operator=(S s) { swap(*this, s); }
destructor (deep copy of your resource)
std::swap
uses move (or copy) constructor, which would lead to infinite recursion.Which function is the half?
From previous article:
"To implement the Copy-Swap idiom your resource management class must also implement a swap() function to perform a member-by-member swap (there’s your “…(and a half)”)"
so the swap
method.
Are there any disadvantages or warnings for this approach, compared to the Rule of Five?
The warning I already wrote is about to write the correct swap to avoid the infinite recursion.