So Move semantics is great, giving us higher performence.
I read this was a completly new feature and without C++11 it is 'impossible' to do this.
But could we do this before C++11? Like something that goes like the below.
class AAA
{
private:
int* m_data;
public:
AAA(int _data) : m_data(new int(_data)) { }
AAA(AAA& _other) // Not using RValue reference.
{
m_data = _other.m_data;
_other.m_data = nullptr;
}
~AAA() { delete m_data; }
};
int main()
{
AAA sth1(100);
AAA sth2(sth1)
return 0;
}
I thought that what the existence of RValue reference is for is not to make the same functions whose argument is just slighty different(like Const and Non-Const).
Simply, with RValue reference which is just another 'type', we can do both Copy constructor and Move constructor at the same time. And that's it.
Wondering if I am correct or am missing something huge.
Thanks in advance.
std::auto_ptr did exactly what you suggest. The problem was that it made behavior confusing and copying impossible. Rvalue references allow one to make classes that can be both moved and copied.
rvalue references allow automatic moving/copying based upon context (for example the moving of a temporary) trying to simulate this with an lvalue style copy constructor (which actually performed a move) would likely be disastrous.
My understanding is that move semantic is not only possible but also available as library (Boost.Move) for C++03 compilers (maybe with some restrictions). However, the language implementation might have been necessary both to allow free optimization without touching anycode and to simplify it's use as much as possible.
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