-
Notifications
You must be signed in to change notification settings - Fork 259
[SUGGESTION] Mark moved-from objects as unitialised #246
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
Comments
It's not always necessary to assign to a moved-from object before using it again. For example, standard library objects are left in a "valid but unspecified state" (unless specified otherwise). This means you can call any of its member functions as long as they don't have preconditions. I.e. the state is valid, but we can't be sure exactly what it is. User defined types might follow that same pattern, or have a stronger guarantee. Here's an example from cppreference:
The So rather than preventing use of moved-from objects without reassigning first, I think the suggestion would have to be: prevent calling a member function with preconditions on a moved-from object, until the moved-from object is placed back into a well-known state. I'm not sure if such a feature is possible, except for hard-coding for specific well-known types. |
I agree with @bluetarpmedia, since moved-from objects are not actually uninitialized, marking their lifetime as sich would break expectations, and if we would want to allow only specific set of functions to be used with moved-from objects, that would need native language support. One way, however could be to have a Cpp2-only destructive move, that would essentially immediately destroy an object upon move, so that such objects could be marled as last use. |
I did originally consider to suggest about destructive move. But ultimately decided on this as destructive move prevents us from reusing the object but I'll be happy with destructive move if it is implemented. As for the other suggestion, I don't think it is feasible but again, I'll be happy with it too. But I would say that sometimes we have to reject some well-formed code to detect errors/undefined behaviour. That's the case with static analysers too in some cases. |
What's the use case for allowing people to call such methods on moved-from objects? Is there an advantage of |
In general case, assigning a string is most likely to be less efficient than clearing it (Jason Turner made a great video about it some time ago). In case of a moved-from string, which might be already cleared by the move itself, the compiler might see it and optimize the clear into a no-op. It also signifies intent more clearly, and to me at least, assigning |
I don't know much about compiler optimisation but can't the compiler also optimise |
Quoted from #221
Excerpt from above
That emphasizes my point of this suggestion, if the caller "shouldn't try to use the object's value again before first resetting it to a new value", then it just makes sense to not let them do that. And this can be extended to move-from assignments too. |
There are many ways to "reset to a new value" (i.e. place the moved-from object back into a well-known state). If I understand correctly, your proposal assumes that only an assignment after the move can reset it to a new value. But in the example I posted above (from cppreference) the |
I agree with you, this proposal is definitely about rejecting some perfectly-formed code to detect errors in related space. |
Closing as by-design and explained in the comments... moved-from is not the same as uninitialized, and I am interested in catching use-of-moved-from but the Lifetime analysis is where I am to do that rather than the guaranteed initialization rules. |
This is a simple suggestion, track the use of moved-from objects just like cppfront does with unitialised objects so that there is a compile time error when using a moved-from object without an assignment after the move.
This is what we teach today, don't use moved-from objects without reassigning to them.
This will prevent errors due to access of moved-from objects and should be good enough reason to implement.
As a side note, what are the performance effects (both compile and run time) of tracking unitialised objects? And can we reuse the objects passed to function via move/forward?
The text was updated successfully, but these errors were encountered: