加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C移动赋值触发器首先移动构造函数

发布时间:2020-12-16 09:53:19 所属栏目:百科 来源:网络整理
导读:#include iostream class C {public: ~C() { std::cout this " destructorn"; } C() { std::cout this " constructorn"; } C(C rhs) { std::cout rhs " rhsn"; std::cout this " move constructorn"; } C operator=(C rhs) { std::cout rhs " rhsn"; st
#include <iostream>    

class C {

public:

  ~C() { std::cout << this << " destructorn"; }

  C() { std::cout << this << " constructorn"; }

  C(C&& rhs) {
    std::cout << &rhs << " rhsn";
    std::cout << this << " move constructorn";
  }

  C& operator=(C&& rhs) {
    std::cout << &rhs << " rhsn";
    std::cout << this << " move assignmentn";
    return *this;
  }

};

C make_one() {
  C tmp;
  return tmp;
}

int main() {

  std::cout << "move constructor:n";
  C c1(make_one());
  std::cout << &c1 << " &c1nn";

  std::cout << "move assignment:n";
  C c2;
  c2 = make_one();
  ...
}

输出:

move constructor:
000000000021F9B4 constructor         // tmp constructed in make_one()
000000000021F9B4 rhs                 // return triggers tmp being passed to ...
000000000021FA04 move constructor    // ... c1's move constructor (see below)
000000000021F9B4 destructor          // tmp destructs on going out of scope
000000000021FA04 &c1                 // (confirmed c1's address)

move assignment:
000000000021FA24 constructor         // c2 constructed
000000000021F9B4 constructor         // tmp constructed in make_one() again
000000000021F9B4 rhs                 // tmp passed to ...
000000000021FA34 move constructor    // ... a new object's move constructor
000000000021F9B4 destructor          // tmp destructs on going out of scope
000000000021FA34 rhs                 // new object passed to ...
000000000021FA24 move assignment     // .. c2's move assignment operator
000000000021FA34 destructor          // new object destructs
...

移动分配似乎首先触发移动构造函数并创建一个额外的对象.这是正常的吗?我希望(通过类比复制赋值)将tmp直接传递给c2的移动赋值.

[Visual Studio Express 2013]

解决方法

“额外对象”称为返回值.从函数返回值时;此值是从您提供给return语句的值构造的复制/移动.

通常这会经历复制省略,这可以解释为什么你不认识它.当复制省略发生时,行C tmp;实际上会将tmp直接构造成返回值.复制省略也可以在其他一些情况下发生;全文见C 11 [class.copy]#31.

大概你可以在这里手动禁用复制省略,或者编译器决定不执行复制省略是个好主意.更新:您的编译器仅在发布版本上执行此特定的copy-elision – 感谢Praetorian

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读