


Does Self-Move Assignment In C 11 Standard Library Functions Have Any Guarantees?
Nov 09, 2024 pm 09:27 PMSelf 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; }
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C language function format letter case conversion steps

What are the types of values returned by c language functions? What determines the return value?

What are the definitions and calling rules of c language functions and what are the

How does the C Standard Template Library (STL) work?

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?
