Home >Backend Development >C++ >When Does Returning an Object by Value Trigger a Move Constructor?
Returning an Object of a Class from a Function by Value
Consider the case where you return an object of a class from a function by value. In this scenario, the returned object is typically considered an lvalue, meaning it has a name and an address in memory. However, certain circumstances can result in the returned object being treated as an rvalue, a temporary object without a name or address.
The Implicit Move Rule
C defines an implicit move rule that may apply when returning an object by value. This rule states that if the following conditions are met:
In such cases, the move constructor will be invoked instead of the copy constructor. This behavior is intended to optimize performance by avoiding unnecessary copying.
Example 1
In this example, the returned object i is an lvalue, so the copy constructor test(const test& z) is invoked:
<code class="cpp">class test { public: test(int y) { printf("test(int y)\n"); } test() { printf("test()\n"); } test(const test& z) { printf("test(const test&z)\n"); } }; test Some_thing() { test i; return i; }</code>
Output:
test() test(const test&z)
Example 2
However, in the following example, the returned object i is treated as an xvalue, and the move constructor test(test&& s) is invoked because the object is temporary and the move constructor is viable:
<code class="cpp">class test { public: test(int y) { printf("test(int y)\n"); } test() { printf("test()\n"); } test(test&& s) { printf("test(test&& s)\n"); } }; test Some_thing() { test i; return i; }</code>
Output:
test() test(test&& s)
Example 3
If a move constructor is not provided or is explicitly deleted, the implicit move rule cannot be applied, and the program will fail to compile:
<code class="cpp">class test { public: test(test&& z) = delete; test(int y) { printf("test(int y)\n"); } test() { printf("test()\n"); } test(const test& z) { printf("test(const test&z)\n"); } }; test Some_thing() { test i; return i; }</code>
Conclusion
When returning an object by value, the implicit move rule may be applied under specific conditions, resulting in the move constructor being invoked. Understanding this behavior is crucial for optimizing code and preventing compilation errors.
The above is the detailed content of When Does Returning an Object by Value Trigger a Move Constructor?. For more information, please follow other related articles on the PHP Chinese website!