-
Notifications
You must be signed in to change notification settings - Fork 462
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
Reference<T> should have noexcept move constructor and move assignment operator #805
Comments
Note that the current implementation of move assignment cannot be noexcept since it destroys the object first (with a potentially throwing Reset()), instead of using swaps (preferably with ADL) which would allow nothrow behaviour. Current code: template Should be: template The moved-from object will then take care of releasing resources when destroyed. |
@szali would you like to create PR implementing your suggestion? |
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made. |
I'd be interested in taking this up as it is related to exceptions. |
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made. |
Hi @szali , We looked at this in today's Node-API meeting. The move assignment cannot be no-except because the reference in the to-be-assigned-into value needs to be deleted. As you noted, this operation can throw. Unless we are misunderstanding something...? |
@KevinEady please see my comment above about implementing move semantics in a noexcept way with swap. This is how it is usually done. The deletion is then deferred until the moved-from value is destroyed. The argument about the resource having to be deleted could be applied to e.g. std::vector, but it nevertheless has nothrow move assignment, see https://en.cppreference.com/w/cpp/container/vector/operator%3D. In fact, having to implement a move assignment operator implies (or should imply) that the class manages a resource, so if your argument were true then noexcept move would not exist. |
@szali I think I see, for my understanding then, by "the deletion is then deferred until the moved-from value is destroyed", you mean:
correct? |
@KevinEady yes |
@szali based on our discussion in the team meeting. The concern with your suggestion is:
|
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made. |
I have a class which has a Napi::Reference member. This class of mine defines all 6 operations (ctor, dtor, copy ctor, move ctor, copy assignment, move assignment). I cannot declare the move operations noexcept since Napi::Reference has non-noexcept move operations, but since I support the copy operations (via own logic), std::vector cannot use my class efficiently.
The point is, Reference should have a noexcept move constructor and move assignment operator to facilitate usage with std containers, since it has only two handles and one bool member, which are trivial to assign, swap and zero out.
The text was updated successfully, but these errors were encountered: