Here you can see copy assignment operator implementation with self assignment check:
String & operator=(const String & s)
{
if (this != &s)
{
String(s).swap(*this); //Copy-constructor and non-throwing swap
}
// Old resources are released with the destruction of the temporary above
return *this;
}
This is good for self assignment, but bad for performance:
- as each time it check as if statement (I don't know how much will be it optimal, considering the branch prediction)
- Also we lose here copy elision for rvalue arguments
So I still don't understand if I would implement std::vector
's operator=
how I would implement it.