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 type int&
  • t int

or

const float value = 5.25; somefunction(value); 

then

  • value of type const 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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -