Home  >  Article  >  Backend Development  >  Does self-move assignment work with `std::vector` in the C Standard Library?

Does self-move assignment work with `std::vector` in the C Standard Library?

Barbara Streisand
Barbara StreisandOriginal
2024-11-11 17:23:03153browse

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::operator=(vector&& other), it is permissible to assume that other is a prvalue. Consequently, if other is a prvalue, self-move-assignment is not feasible.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn