In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use RVO and NRVO in C++". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use RVO and NRVO in C++".
1. Mobile operation 1. Functions related to mobile operation
There are two class functions related to mobile operations
Move the constructor:
A (A & rhs)
Move the assignment operator:
A & operator= (A & rhs)
Note that neither of these functions takes an argument of type const, which is the function declaration that C++ generates by default.
The mobile constructor is used when constructing a type to use:
AA / use std::move to force movement An a2 = std::move (A1); or Aa2 (std::move (A1))
The move assignment operator moves at the time of assignment:
A1 to A1 / a2 to A1 = std::move (a2); / / use move to force movement 2, when to automatically declare movement constructor and assignment movement constructor
The implicit mobile constructor will be automatically generated if it can be generated and meets all of the following conditions:
Copy constructor without user declaration
No user-declared copy assignment operator (that is, operator= (const A &))
There are no user-declared mobile assignment operators (that is, operator= (invalid &) and the like)
Destructor without user declaration
What can be generated means that all the following conditions are met:
There are no non-static members in the class that cannot be moved
When inheriting, the base class can be moved
When inheriting, the constructor of the base class can be accessed
The conditions for the generation of the mobile assignment operator are similar, except that the undeclared mobile assignment constructor is changed to the undeclared mobile constructor.
In short, the conditions for the generation of these two functions are in one sentence: except for ordinary constructors (default constructors and constructors with other parameters), no other constructors, operator= functions and destructors can be declared.
3. When to move automatically
Using std::move is a mandatory, explicit move. But C++ often helps us move automatically for the sake of efficiency. The main rule is that all right values are moved, and if they cannot be moved, they are copied. But for the sake of rigor, let's lay out the rules on cppreference:
Initialize with std::move (): t a = std::move (b) or T a (std::move (b)); this. Add std::move () here, otherwise the copy constructor will be called.
Use std::move (): func (std::move (a)) when passing function arguments
When the function returns, such as:
Class A {}; A CreateA () {return A ();} / / callA a = CreateA ()
When you use A (), the variable generated by A () is first moved to the return value generated by the CreateA () function, which is a temporary variable (we call it temp). The next step is to execute this code: AA = temp, then temp is a temporary variable, and the moving constructor of An is called again to the a variable.
The first two are explicit movements, and the last is implicit movement. The same is true for moving assignment operators, where only temporary variables to the right of the equal sign are called automatically.
II. Replication elimination, RVO and NRVO
Although C++ has a clear definition of mobile operations, compilers do not always follow this definition. Because there are three important optimizations in the compiler that often reduce copy or even move operations.
The-fno-elide-constructors option can be added under GCC and Clang to turn off these three optimizations.
1. Copy elimination
Take a look at the following code:
Class C {public: C () {} C (const C &) {std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.