繁体   English   中英

未调用移动构造函数和复制构造函数

[英]Move-ctor and copy-ctor not called

让我们以以下 C++ 示例为例:

#include <iostream>

struct X
{
  std::string s;

  X() : s("X") { }

  X(const X& other) : s{other.s} { std::cout << "cpy-ctor\n"; }

  X(X&& o): s{o.s} { o.s = ""; std::cout << "move-ctor\n"; }

  X& operator=(const X& other) {
    std::cout << "cpy-assigned\n";
    s = other.s;
    return *this;
  }

  X& operator=(X&& other) {
    if (this != &other) {
      s = other.s;
      other.s = "";
    }
    std::cout << "move assigned\n";
    return *this;
  }
};

X f(X x) {
  std::cout << "f: ";
  return x;
}

X g() {
  std::cout << "g: ";
  X x;
  return x;
}

int main() {
  X x;
  X y;
  x = f(X());
  y = g();
}

如果我用 gcc 4.8.2 编译它,我有以下结果:

f: move-ctor
move assigned
g: move assigned

我不明白为什么在调用 g 函数时不调用复制构造函数。

我只是想了解何时调用复制或移动构造函数。

虽然你是正确的,以确定有,在逻辑上,复制/局部变量的移动x从内g()返回时,C ++的一个非常有用的功能是它可以的Elid(即跳过)在许多情况下,这种操作,即使复制/移动会产生副作用 这是其中一种情况。 执行时,这称为命名返回值优化

可以说它不如我们有移动语义之前有用,但它仍然很好。 事实上, C++17 在某些(选择)情况下强制执行

在 C++ 中,所有表达式都是:

  • 左值
  • 右值

构造对象

- 左值

Y x{};
Y y{x}; // copy constructor, x is an lvalue

- 右值 - RVO

使用 RVO,默认情况下由 gcc 启用。 这省略了使用复制构造函数,而是将对象构造一次。

X g()
{
  X x {}; 
  x.value = 10;
  return x;
}

X y {g()}; // X constructor get's called only once to create "y". Also 
           // y is passed a a reference to g() where y.value = 10. 
           // No copy/move constructor for optimization "as if" rule

- 纯右值 - 无 RVO

如果没有 RVO,在这种情况下取​​决于。 如果移动构造函数被显式或隐式删除,那么它将调用复制构造函数

复制构造函数

struct X { X(const X&) {}}; // implicitly deletes move constructor 
                            // and move assignment, see rule of 5

X g()
{
  return X{}; // returns a prvalue
}

X y {g()}; // prvalue gets converted to xvalue, 
           // "temporary materialization", where the xvalue has an 
           // identity where members can be copied from. The xvalue
           // binds to lvalue reference, the one from copy constructor
           // argument

移动构造函数

X { X(X&&) {}}; // explicitly declared move constructor

X g()
{
  return X{}; // returns a prvalue
}

X y {g()}; // prvalue gets converted to xvalue, 
           // "temporary materialization", where the xvalue has an
           // identity where members can be moved from. The xvalue 
           // binds to rvalue reference, the one from move constructor 
           // argument

- 值

X x {};
X y {std::move(x)}; // std::move returns an xvalue, where if move 
                    // constructor is declared will call it, other wise 
                    // copy constructor, similar to explained above for 
                    // prvalue.

复制/移动分配

- 左值

X x{};
X y{};

x = y; // call copy assignment operator since y is an lvalue.

- 右值

如果显式或隐式删除了移动赋值,则它将调用复制赋值运算符。

复制作业

struct X{ X& operator=(const X&); } // implicilty deletes move 
                                    // constructor and move assignment,
                                    // see rule of 5

X g()
{
  return X{}; // returns a prvalue
}

x = g(); // prvalue gets converted to xvalue, 
       // "temporary materialization", where the xvalue has an identity 
       // where members can be copied from. The xvalue binds to lvalue 
       // reference, the one from copy assignment operator argument

移动分配

struct X{ X& operator=(X&&); } // explicitly declared move assignment        operator

X g()
{
  return X{}; // returns a prvalue
}

x = g(); // prvalue gets converted to xvalue, 
       // "temporary materialization", where the xvalue has an identity 
       // where members can be moved from. The xvalue binds to rvalue 
       // reference, the one from move assignment operator argument

- 值

X x {};
X y {};

x = std::move(x); // std::move returns an xvalue, where if move
                  // assignment is declared will call it, other 
                  // wise copy assignment, similar to explained 
                  // above for prvalue. 

当您使用另一个相同类型的对象实例化一个对象时,将调用复制构造函数。

例如:

X x;
X y(x);

代码中的最后一行将从函数返回的值分配给已构造的对象。 这是通过移动分配完成的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM