gpt4 book ai didi

c++ - 在 RVO 存在的情况下默认/删除移动构造函数和赋值

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:18 25 4
gpt4 key购买 nike

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

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

#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 &&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

后来发现可能是返回值优化导致的,所以又用-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 constructor 仍然会导致程序编译失败。

我的问题:

  • 为什么删除移动构造函数会报错? 为什么它不匹配 copy constructor 而不是(尽管不像 move constructor 那样完全匹配)? C++03 没有右值引用,那么编译器的解决方案是什么?另外,我读了another question我想在我的例子中,因为我指定了用户声明的复制构造函数 移动构造函数 不应该 默认(因此它应该被删除?)我也意识到n3376对此有类似的描述。 clang是否符合标准?

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

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

最佳答案

  1. 即使编译器省略了复制或移动,该语言仍然要求该函数存在且可访问。这是为了让程序始终一致地编译,无论编译器是否省略了特定的复制/移动。

  2. test 有一个名称,因此它自动成为一个左值,并且不能在没有显式 std::move 的情况下移动。因此,编译器必须将 test 复制到 t 中。

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

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

关于c++ - 在 RVO 存在的情况下默认/删除移动构造函数和赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25811845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com