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?

Nov 09, 2024 pm 09:27 PM

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!

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

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 What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

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

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

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

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

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

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

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

See all articles