Consider the following code. In order for this to compile, I HAVE to provide a body for the move constructor. Having "TestClass(TestClass&& other) = default" does not compile with an error that says "TestClass::TestClass(TestClass&&)' is implicitly deleted because the default definition would be ill-formed:"
Why is this? Why can't it figure out the default?
#include <iostream>
#include <optional>
#include <boost/noncopyable.hpp>
class TestClass : public boost::noncopyable {
public:
TestClass() {
std::cout << "Constructed...\n";
}
TestClass(TestClass&& other) = default;
//TestClass(TestClass&& other) {
// std::cout << "Move constructor...\n";
//}
TestClass& operator=(TestClass&& other) {
std::cout << "Move assignment...\n";
return *this;
}
};
TestClass doThing() {
std::optional<TestClass> tc = std::make_optional<TestClass>(TestClass{});
return std::move(*tc);
}
int main() {
const TestClass t = doThing();
}
https://godbolt.org/z/3f5zTT6a9
CodePudding user response:
boost::noncopyable
originates from before C 11. So maybe the name isn't making this obvious, but it doesn't only make the class non-copyable, it also makes it non-movable (with move semantics having been added only with C 11). If the base class can't be moved, then a default implementation of the move constructor is not possible.
Also it should be used by private
inheritance, not public
.
CodePudding user response:
Yeah, I think the misnomer of boost::noncopyable (not nonmovable) confused me. Thanks!