-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
C.21 Exception for copy-and-swap idiom #2164
Comments
I don't think it is worth to be an exception in C.21. Such the copy-and-swap idiom should be avoided: it performs copy and swap for lvalues and move and swap for rvalues, that is not optimal. And additional calls to |
No, they are not. The other object gets copied or moved-from just to pass the T& T::operator=(T other) noexcept {
swap(other);
return *this;
} Also, while I'm not a huge fan of this kind of assignment operator myself, if we're not going to add an exception to C.21 for this, then we should add a rule which advises against such assignment operators in the first place. The current wording just says nothing on this, making it unclear how the corresponding clang-tidy rule should be implemented. |
If |
Yes, but why would the move constructor be missing? That would still be a violation of C.21 and I don't seek to change that, so I don't see how this point is relevant. This reminds me that an exception to C.60: Make copy assignment non-virtual, take the parameter by |
C.21 is missing an exception for the copy-and-swap idiom. It's perfectly safe (and considered idiomatic by many) for resource management to write:
This would violate C.21 since technically, the user has only defined the copy assignment operator, and the move assignment operator is implicitly not declared.
The lack of an exception to this rule also manifests itself in clang-tidy false positives. See:
operator=(T)
llvm/llvm-project#76064The text was updated successfully, but these errors were encountered: