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.