Why do we need a second std :: forward specialization?

As far as I understand from this explanation of how http://thbecker.net/articles/rvalue_references/section_08.htmlstd::forward works , we can refuse only one version :std::forward

template<class S>
S&& forward(typename remove_reference<S>::type& a) noexcept

But actually we have ( http://en.cppreference.com/w/cpp/utility/forward ) the second version:

template< class T >
T&& forward( typename std::remove_reference<T>::type&& t );

which differs from the previous one only in how it tis determined (c &&)

So why do we need this? What will break if you remove it?

+4
source share
1 answer

std::forward ( std::remove_reference) , lvalue rvalue . const, ( ) T (.. const T&, const rvalue, T&& , rvalue).

A rvalue; , , std::forward lvalue rvalue; , std::forward<int&>(42); . std::forward std::forward<T>, T - T&&, reference. , .

std::forward " ", Going Native 2013. , std::forward ( 20- );

template <typename T>
T&& forward(T&& param) { // T&& here is formulated to disallow type deduction
  if (is_lvalue_reference<T>::value) {
    return param; // return type T&& collapses to T& in this case
  }
  else {
    return move(param); // return type is T&&
  }
}

std::forward lvalue, lvalue rvalue ( std::move), rvalue.


TL; DR ? ; std::forward (, , , / ..).

+4

All Articles