Why does forward need a remove_reference?

Hi,

Looking at the definition of std::forward, I do not understand why it is necessary to specify a remove_reference_t in the first and only argument, like so:


1
2
3
4
5
template <class _Ty>
constexpr _Ty&& forward(
    remove_reference_t<_Ty>& _Arg) noexcept { // forward an lvalue as either an lvalue or an rvalue
    return static_cast<_Ty&&>(_Arg);
}


because _Ty could be a for some type C, equal to C& or C&& and by the collapsing rules both would return a C&, which is the same as applying remove_reference_t.


Am I clear?


Regards,
Juan
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3143.html that introduced the change (and the papers linked from it, for context)
Last edited on
Topic archived. No new replies allowed.