Home > Article > Backend Development > Does self-move assignment work with `std::vector` in the C Standard Library?
Self Move Assignment in the C Standard Library
In C 11, the standard provides certain guarantees regarding self move assignment, particularly within the standard library. Let's investigate what those assurances entail.
According to Section 17.6.4.9 of the C 11 standard, under the "Function Arguments" topic, it is stated that if a function argument is bound to an rvalue reference parameter, the implementation can assume that this reference is exclusive to the argument.
This implies that within the implementation of std::vector
What is likely to occur is that v will be rendered resource-less (with zero capacity). This action will be a no-op if v already has zero capacity.
Update:
The most recent working draft (N4618) offers a more precise definition of the MoveAssignable requirements. It specifies that the expression t = rv (where rv is an rvalue) requires that t only match the value of rv before the assignment if they do not refer to the same object. Regardless, rv's state becomes unspecified after the assignment. An additional note provides further clarification:
"rv must still meet the requirements of the library component that is using it, whether or not t and rv refer to the same object."
The above is the detailed content of Does self-move assignment work with `std::vector` in the C Standard Library?. For more information, please follow other related articles on the PHP Chinese website!