Home > Backend Development > C++ > body text

Why isn't the C 11 Move Constructor Called When Creating an Object from a Temporary Object?

Patricia Arquette
Release: 2024-11-04 19:17:02
Original
215 people have browsed it

Why isn't the C  11 Move Constructor Called When Creating an Object from a Temporary Object?

C 11 Move Constructor Not Called, Default Constructor Preferred

In certain scenarios, the C 11 move constructor may not be invoked, favoring the default constructor instead. To understand why, let's examine an example.

Consider the following class:

<code class="cpp">class X {
public:
    explicit X (char* c) { cout << "ctor" << endl; init(c); };
    X (X& lv)  { cout << "copy" << endl;  init(lv.c_); };
    X (X&& rv) { cout << "move" << endl;  c_ = rv.c_; rv.c_ = nullptr; };

    const char* c() { return c_; };

private:
    void init(char *c) { c_ = new char[strlen(c)+1]; strcpy(c_, c); };
    char* c_;

};</code>
Copy after login

With this class, we can create objects as follows:

<code class="cpp">int main() {
    X x("test");
    cout << x.c() << endl;
    X y(x);
    cout << y.c() << endl;
    X z( X("test") );
    cout << z.c() << endl;

    return 0;
}</code>
Copy after login

The expected output would be:

ctor
test
copy
test
ctor  <-- Why not move?
test
Copy after login

However, we observe that the move constructor is not called in the last line. Instead, the default constructor is used. To explain this, we need to understand copy elision.

Copy elision is an optimization technique allowed by the C 11 standard under certain conditions. It allows the compiler to construct a temporary object directly into the target object, avoiding the copy/move constructor and destructor overhead.

In this example, the X object created from the temporary "test" is elided into z. This means that no copy/move constructor is called, and instead, the object is constructed directly into z.

The compiler may perform copy elision when all of these conditions are met:

  • The object is a temporary object that has not been bound to a reference.
  • The object is copied/moved to an object of the same cv-unqualified type.

In our case, these conditions are satisfied, and thus, copy elision occurs, causing the default constructor to be used instead of the move constructor. To explicitly call the move constructor, you can use std::move as seen in:

<code class="cpp">X z( std::move(X("test")) );</code>
Copy after login

The above is the detailed content of Why isn't the C 11 Move Constructor Called When Creating an Object from a Temporary Object?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!