Home > Backend Development > C++ > When Does Returning an Object by Value Trigger a Move Constructor?

When Does Returning an Object by Value Trigger a Move Constructor?

Patricia Arquette
Release: 2024-11-04 08:51:30
Original
415 people have browsed it

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:

  • The returned expression is an xvalue (an expression denoting a temporary object that is not an lvalue).
  • The class of the returned object has a move constructor.
  • The move constructor is accessible.

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&amp; z) { printf("test(const test&amp;z)\n"); }
};
test Some_thing() {
    test i;
    return i;
}</code>
Copy after login

Output:

test()
test(const test&amp;z)
Copy after login

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&amp;&amp; s) { printf("test(test&amp;&amp; s)\n"); }
};
test Some_thing() {
    test i;
    return i;
}</code>
Copy after login

Output:

test()
test(test&amp;&amp; s)
Copy after login

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&amp;&amp; z) = delete;
    test(int y) { printf("test(int y)\n"); }
    test() { printf("test()\n"); }
    test(const test&amp; z) { printf("test(const test&amp;z)\n"); }
};
test Some_thing() {
    test i;
    return i;
}</code>
Copy after login

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template