New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why a copy is performed in move assignment implementation? #2
Comments
If it were just Is this correct? Edit: this illustrates the difference http://ideone.com/IqXSRy |
Yes, exactly. When you use assignment you fully expect the left hand side to be lost, not given to the right hand side. Giving |
Cool, thanks. |
I end up writing move assignment as just: module& operator=(module that) {
std::swap(handle, that.handle);
return *this;
} |
@gnzlbg would it work if the |
Sure. If the module is only movable then |
@gnzlbg It's not clear to someone else looking at your code that it's a move assignment since it doesn't follow the usual signature. |
@developerbmw if you just need to make a type copy/move assignable fast, and the type already has copy/move constructors, this is a not-very-efficient but very simple solution that allows you to reuse all of the copy/move constructors code. Readers that just want to use the type are typically only interested on knowing if the type is at least If the reader is interested in knowing exactly how the type works and doesn't know this idiom you make a very good point, but arguably the same argument can be made about other idioms like copy-and-swap, and a similar argument can be made about any type that has user defined special member functions (the reader has to work through them anyways). |
In the following article https://rmf.io/cxx11/rule-of-zero/ you implemented a move assignment in this way:
What purpose does the copying here serve? Could it not just be
std::swap(handle, that.handle);
?Thanks
The text was updated successfully, but these errors were encountered: