I guess that this question has been asked but I simply haven't found similar answers yet.
Let's see a contrived example.
#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;
}
When I explicitly Test(Test &&test) = delete;
and compiled this segment with clang-3.4
it emit errors like below:
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;
When I customize the move constructor like the one in the src, it compiles. However
However NO move constructor is even called(here is the result):
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
Later I found it may result from return value optimization
so I compiled again with -fno-elide-constructors
. This time the result is as below:
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
It invoked move constructors
as expected. However explicitly deleting move constructor
still causes the program to fail to compile.
my questions:
Why it reports errors when the
move constructor
is deleted? Why DIDN'T it matchcopy constructor
instead(although not exact match as themove constructor
did)?C++03
has no rvalue-reference, what was the compilers solutions then? In addition, I read another question and I suppose that in my case since I specified a user-declared copy constructor the move constructor shouldn't be default(thereby it should be deleted?) I also realized that n3376 has similar writes about this. Is clang accord with the standard?What has
Return Value Optimization
done here?Especially, why(Sorry I didn't notice that there's only one copy constructor invocation in the RVO version results)Test::Test(const Test &)
is called rather thanTest::Test(Test &&)
?I also noticed similar issues for
move assignment
(if it's deleted, then compiles complains error). So what's going on here?