I have the following code:
#include <cstdio>
#include <iostream>
using std::cout;
struct SomeType {
SomeType() {}
SomeType(const SomeType &&other) {
cout << "SomeType(SomeType&&)\n";
*this = std::move(other);
}
void operator=(const SomeType &) {
cout << "operator=(const SomeType&)\n";
}
void operator=(SomeType &&) {
cout << "operator=(SomeType&&)\n";
}
};
int main() {
SomeType a;
SomeType b(std::move(a));
b = std::move(a);
return 0;
}
I expect move constructor to call the move assignment operator. Here is the output of this program:
SomeType(SomeType&&)
operator=(const SomeType&)
operator=(SomeType&&)
As you can see, move assignment operator is successfully called, but not when assigning to *this
inside move constructor. Why is this happening, can I fix it somehow?
Your move constructor takes a const SomeType&&
rather than a SomeType&&
. You cannot call a function that takes a SomeType&&
(your move constructor) with a value of type const SomeType&&
.
Try making a move constructor that takes a SomeType&&
.
std::move
casts to an r-value reference. So it casts other
to const SomeType &&
. This of course cannot bind to SomeType &&
, so it falls back to const SomeType &
.
Remove the const
from the move constructor's parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With