Home > Backend Development > C++ > Does Self-Move Assignment In C 11 Standard Library Functions Have Any Guarantees?

Does Self-Move Assignment In C 11 Standard Library Functions Have Any Guarantees?

DDD
Release: 2024-11-09 21:27:02
Original
282 people have browsed it

Does Self-Move Assignment In C  11 Standard Library Functions Have Any Guarantees?

Self Move Assignment in C 11 Standard Library

The C 11 standard encompasses various provisions regarding self move assignment, particularly in conjunction with the standard library. This article elucidates these provisions, focusing on the implications of self move assignment in library functions.

Specifically, the standard guarantees that when an argument in a standard library function is an rvalue reference parameter, the implementation can assume its exclusivity. This means the implementation can optimize the code based on the understanding that the passed object is not modified by the function.

In the context of self move assignment, this guarantee has a noteworthy impact. For instance, consider the following function:

template<class T>
std::vector<T> selfAssign(std::vector<T> v) {
  v = std::move(v);
  return v;
}
Copy after login

When passed an lvalue, the vector's copy constructor is invoked, creating a copy of the input vector. However, due to the standard library guarantee, the implementation of the vector's move assignment operator can assume that its argument is a prvalue. Thus, self-move-assignment is not feasible, and the function's behavior becomes dependent on the implementation.

In most cases, self-move-assignment leaves the original vector in a resource-less state (0 capacity). If the vector already possesses 0 capacity, the operation becomes a no-op. However, it's crucial to note that the states of both the original and moved vectors are unspecified after self-move-assignment.

Recent updates to the standard in Working Draft N4618 further clarify these provisions, specifying that in the MoveAssignable requirements, where an rvalue (rv) is assigned to an expression (t), t only needs to attain the value of rv before assignment if t and rv do not refer to the same object. Regardless of this condition, rv's state remains unspecified following the assignment.

Additionally, a note emphasizes that rv must adhere to the requirements of the library component utilizing it, regardless of whether t and rv reference the same object.

The above is the detailed content of Does Self-Move Assignment In C 11 Standard Library Functions Have Any Guarantees?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template