Implementation of std::forward

Hi,

Looking at the definition of std::forward, I found this implementation which does not appear to make sense. It is said that it forwards the argument as an lvalue or an rvalue. How can that be since the return type is an rvalue reference?


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);
}



Thanks
Last edited on
If _Ty is a lvalue reference then _Ty&& is a lvalue reference too.

1
2
3
4
5
6
7
8
9
10
11
12
#include <type_traits>
#include <utility>

static_assert(std::is_same_v<std::add_rvalue_reference_t<int>, int&&>);
static_assert(std::is_same_v<std::add_rvalue_reference_t<int&>, int&>); // <--
static_assert(std::is_same_v<std::add_rvalue_reference_t<int&&>, int&&>);
using lvalue_ref = int&;
using rvalue_ref = int&&;
static_assert(std::is_same_v<lvalue_ref&, int&>);
static_assert(std::is_same_v<lvalue_ref&&, int&>);
static_assert(std::is_same_v<rvalue_ref&, int&>);
static_assert(std::is_same_v<rvalue_ref&&, int&&>);


https://cplusplus.com/forum/general/281910/#msg1219774
Last edited on
Topic archived. No new replies allowed.