Update: I use MSVC10, which doesn't not give me default move-semantics
Let's say I want to create a regular class with a couple of non-pod members;
class Foo {
NonPodTypeA a_;
NonPodTypeB b_;
}
As usual I implement a copy-constructor, and an assignment operator in which I utilize the copy-constructor:
Foo(const Foo& other) : a_(other.a_), b_(other.b_) {}
Foo& operator=(const Foo& other) {
Foo constructed(other);
*this = std::move(constructed);
return *this;
}
Then I implement the move-constructor and move-assignment, which utilizes std::swap instead of std::move for all member's as they might be written before move-semantics were available, as move-semantics is implemented, I can omit implementing a swap member function:
Foo(Foo&& other) {
::std::swap(a_, other._a);
::std::swap(b_, other._b);
}
Foo& operator=(Foo&& other) {
::std::swap(a_, other._a);
::std::swap(b_, other._b);
return *this;
}
And here goes my question; can something here be done more general, assuming I don't know anything about the members?
For example, the move-constructor is not compatible with const declared members, but if I implement the move constructor as Foo(Foo&& other) : a_(std::move(other.a_)), b_(std::move(other.b_)){}
I cant be sure classes without move-semantics are not copied?
Can I utilize the move-constructor in the move-assignment in some clever way?