1

Can someone explain to me how std::forward works in detail. Like it’s code definition and how it works. Also if possible, can you show how the function works and what it does to make it work. Explain it in as much detail as you can please.

Here is the std::forward code definition:

RValue as argument:

template <class _Ty>
_NODISCARD constexpr _Ty&& forward(remove_reference_t<_Ty>&& _Arg) noexcept { // forward an rvalue as an rvalue
    static_assert(!is_lvalue_reference_v<_Ty>, "bad forward call");
    return static_cast<_Ty&&>(_Arg);
}

LValue as argument:

// FUNCTION TEMPLATE forward
template <class _Ty>
_NODISCARD constexpr _Ty&& forward(
    remove_reference_t<_Ty>& _Arg) noexcept { // forward an lvalue as either an lvalue or an rvalue
    return static_cast<_Ty&&>(_Arg);
}

Explain how these two function(these are std::forward functions but with different parameters) works when a argument of either LValue reference or RValue reference is passed.

  • where did you take the code from? There is no "the `std::forward` code definition" – 463035818_is_not_an_ai Jun 24 '21 at 20:16
  • 1
    I found https://stackoverflow.com/questions/8526598/how-does-stdforward-work by putting `std::forward` into a search engine. What happened when you tried this? What exactly do you find unsatisfactory about the prior answers? – Karl Knechtel Jun 24 '21 at 20:18

0 Answers0