c++ - why std::move uses std::remove_reference? -
according http://en.cppreference.com/w/cpp/utility/move
std::move
declared follows:
template <typename t> std::remove_reference<t>::type&& move(t&& t);
as far understanding goes, when code templated, deduction of t
in typename t
looses information reference, following:
template <typename t> void somefunction(t&& value);
when used like:
int five=5; somefunction(five);
then
value
of typeint&
t
int
or
const float value = 5.25; somefunction(value);
then
value
of typeconst float&
t
const float
.
if so, there no point in move declaration declare returned type as: std::remove_reference<t>::type&&
, because t not reference.
moreover, if std::move
takes argument reference (l-value reference in practice), returning static_cast<t&&>(t)
in std::move
in fact due reference collapsing return l-value reference or r-value reference, behave more std::forward
not move. trick, makes work properly, not understand?
your examples incorrect:
int five=5; somefunction(five);
in case, t
deduced int&
, not int
. same goes second example; t
deduced const int&
.
as such, returning t&&
mean t&& &
, t&
due reference collapsing rules.
this why std::remove_reference
required, ensure there no references on type prevent reference collapsing taking place.
Comments
Post a Comment