【发布时间】: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 &&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在这里做了什么?特别是,为什么调用(抱歉,我没有注意到 RVO 版本结果中只有一个复制构造函数调用)Test::Test(const Test &)而不是Test::Test(Test &&)?我还注意到
move assignment的类似问题(如果它被删除,则编译会报错)。那么这里发生了什么?
【问题讨论】:
-
删除的特殊成员函数参与重载决议,并且需要移动或复制构造函数,即使 RVO 省略了副本。
-
如果您提供了复制 ctor 但没有移动 ctor,则移动 ctor不被删除。相反,它根本没有声明。
-
程序必须在没有优化的情况下有效,无论是否进行优化。
-
@HongxuChen:不行,不能调用既不存在也不说不存在的函数。
-
@HongxuChen:不,代码中从未提及未声明的函数,也不以任何方式、形状或形式存在。 它不存在。
deleted函数“存在”,但在您尝试使用它时会报告错误。 (你说得对,它非常奇怪和愚蠢,我认为deleted的名字很糟糕。)
标签: c++ c++11 return-value rvalue-reference return-value-optimization