【问题标题】:default/delete move constructor and assignment with the existence of RVO存在 RVO 的默认/删除移动构造函数和赋值
【发布时间】:2014-09-12 15:32:41
【问题描述】:

我猜有人问过这个问题,但我还没有找到类似的答案。

让我们看一个人为的例子。

#include <iostream>
#include <string>
#include <cassert>

#define LOG \
  std::cout << __PRETTY_FUNCTION__ << ' ' << str_ << '\t' << this << std::endl;

    class Test {
     public:
      Test(std::string const &str) : str_(str) { LOG; }
      Test(Test const &rhs) : str_(rhs.str_) { LOG; }
      // Test(Test &&rhs) = delete;
      Test(Test &&rhs) : str_(std::move(rhs.str_)) { LOG; }
      // Test &operator=(Test const &rhs) {
      // if (this == &rhs) return this;
      //   str_ = rhs.str_;
      //   LOG;
      //   return *this;
      // }
      // Test &operator=(Test &&rhs) = delete;
      // Test &operator=(Test &&rhs) {
      //   assert(this != &rhs);
      //   str_.swap(rhs.str_);
      //   LOG;
      //   return *this;
      // }
      ~Test() { LOG; }
      static Test gen() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
        return Test("DUMMY");
      }

     private:
      std::string str_;
    };

    int main(void) {
      {
        Test test = Test("test");
        Test t(test);
      }
      std::cout << std::endl;
      { Test t0(Test("t0")); }
      std::cout << std::endl;
      {
        Test t1 = Test{"t1"};
        /// t1 = Test("t2");
      }
      std::cout << std::endl;
      { Test t(Test::gen()); }
      return 0;
    }

当我明确地 Test(Test &amp;&amp;test) = delete; 并使用 clang-3.4 编译此段时,它会发出如下错误:

simple.cc:29:12: error: call to deleted constructor of 'Test'
    return Test("DUMMY");
           ^~~~~~~~~~~~~
simple.cc:12:3: note: function has been explicitly marked deleted here
  Test(Test &&rhs) = delete;

当我像 src 中的那样自定义移动构造函数时,它会编译。然而

然而甚至没有调用移动构造函数(结果如下):

Test::Test(const std::string &) test    0x7fff2e513448
Test::Test(const Test &) test   0x7fff2e513430
Test::~Test() test  0x7fff2e513430
Test::~Test() test  0x7fff2e513448

Test::Test(const std::string &) t0  0x7fff2e513428
Test::~Test() t0    0x7fff2e513428

Test::Test(const std::string &) t1  0x7fff2e513410
Test::~Test() t1    0x7fff2e513410

static Test Test::gen()
Test::Test(const std::string &) DUMMY   0x7fff2e5133f8
Test::~Test() DUMMY 0x7fff2e5133f8

后来我发现它可能是由return value optimization引起的,所以我再次使用-fno-elide-constructors编译。这次的结果如下:

Test::Test(const std::string &) test    0x7fff9590cd90
Test::Test(Test &&) test    0x7fff9590cd98
Test::~Test()   0x7fff9590cd90
Test::Test(const Test &) test   0x7fff9590cd78
Test::~Test() test  0x7fff9590cd78
Test::~Test() test  0x7fff9590cd98

Test::Test(const std::string &) t0  0x7fff9590cd68
Test::Test(Test &&) t0  0x7fff9590cd70
Test::~Test()   0x7fff9590cd68
Test::~Test() t0    0x7fff9590cd70

Test::Test(const std::string &) t1  0x7fff9590cd48
Test::Test(Test &&) t1  0x7fff9590cd50
Test::~Test()   0x7fff9590cd48
Test::~Test() t1    0x7fff9590cd50

static Test Test::gen()
Test::Test(const std::string &) DUMMY   0x7fff9590ccf0
Test::Test(Test &&) DUMMY   0x7fff9590cd28
Test::~Test()   0x7fff9590ccf0
Test::Test(Test &&) DUMMY   0x7fff9590cd30
Test::~Test()   0x7fff9590cd28
Test::~Test() DUMMY 0x7fff9590cd30

它按预期调用了move constructors。但是显式删除move constructor 仍然会导致程序编译失败。

我的问题:

  • 为什么删除move constructor会报错? 为什么它不匹配 copy constructor 而是(虽然不完全匹配 move constructor 那样)? C++03 没有右值引用,那么编译器的解决方案是什么?此外,我阅读了another question,我想在我的情况下,因为我指定了用户声明的复制构造函数移动构造函数不应该默认(因此应该删除它?)我还意识到n3376 也有类似的文章。 clang符合标准吗?

  • Return Value Optimization 在这里做了什么? 特别是,为什么调用 Test::Test(const Test &amp;) 而不是 Test::Test(Test &amp;&amp;)(抱歉,我没有注意到 RVO 版本结果中只有一个复制构造函数调用)

  • 我还注意到move assignment 的类似问题(如果它被删除,则编译会报错)。那么这里发生了什么?

【问题讨论】:

  • 删除的特殊成员函数参与重载决议,并且需要移动或复制构造函数,即使 RVO 省略了副本。
  • 如果您提供了复制 ctor 但没有移动 ctor,则移动 ctor被删除。相反,它根本没有声明。
  • 程序必须在没有优化的情况下有效,无论是否进行优化。
  • @HongxuChen:不行,不能调用既不存在也不说不存在的函数。
  • @HongxuChen:不,代码中从未提及未声明的函数,也不以任何方式、形状或形式存在。 它不存在deleted 函数“存在”,但在您尝试使用它时会报告错误。 (你说得对,它非常奇怪和愚蠢,我认为 deleted 的名字很糟糕。)

标签: c++ c++11 return-value rvalue-reference return-value-optimization


【解决方案1】:
  1. 即使编译器省略了复制或移动,语言仍然要求函数存在且可访问。这是为了使程序始终能够始终如一地编译,无论编译器是否忽略了特定的复制/移动。

  2. test 有一个名称,因此它会自动成为一个左值,如果没有明确的std::move,就无法从其移动。因此编译器必须将test 复制到t

  3. 我不明白您期望发生什么 - 我看不出它有什么理由不调用移动赋值运算符。

简短的版本是,如果在优化之前名义上调用了移动或复制构造函数,它们必须存在并且可以访问。这只是语言标准的要求。

【讨论】:

  • 1.我仍然无法理解标准中的move constructor 隐式规则。是否会为 RVO 版本生成此默认移动构造函数?
  • 2.只需删除问题的第二部分。对不起我的错误。
  • 3.这种情况类似于构造函数,当move assignment被删除时,编译器会报错。
  • 基本上我不知道为什么move constructor/assignment 应该存在,即使它们最终没有被调用?
  • 什么时候将移动构造函数从生成的类中删除?
猜你喜欢
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
相关资源
最近更新 更多