I just found myself not fully understanding the logic of std::move()
.
At first, I googled it but seems like there are only documents about how to use std::move()
, not how its structure works.
I mean, I know what the template member function is but when I look into std::move()
definition in VS2010, it is still confusing.
the definition of std::move() goes below.
template<class _Ty> inline
typename tr1::_Remove_reference<_Ty>::_Type&&
move(_Ty&& _Arg)
{ // forward _Arg as movable
return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);
}
What is weird first to me is the parameter, (_Ty&& _Arg), because when I call the function like you see below,
// main()
Object obj1;
Object obj2 = std::move(obj1);
it basically equals to
// std::move()
_Ty&& _Arg = Obj1;
But as you already know, you can not directly link a LValue to a RValue reference, which makes me think that it should be like this.
_Ty&& _Arg = (Object&&)obj1;
However, this is absurd because std::move() must work for all the values.
So I guess to fully understand how this works, I should take a look at these structs too.
template<class _Ty>
struct _Remove_reference
{ // remove reference
typedef _Ty _Type;
};
template<class _Ty>
struct _Remove_reference<_Ty&>
{ // remove reference
typedef _Ty _Type;
};
template<class _Ty>
struct _Remove_reference<_Ty&&>
{ // remove rvalue reference
typedef _Ty _Type;
};
Unfortunately it's still as confusing and I don't get it.
I know that this is all because of my lack of basic syntax skills about C++. I'd like to know how these work thoroughly and any documents that I can get on the internet will be more than welcomed. (If you can just explain this, that will be awesome too).
See Question&Answers more detail:os