How do you comprehend "std::forward is just syntactic sugar"? Is that true? I would appreciate that if you could explain the related code below in detail.
As per the documentation(https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00416_source.html),
here is the implementation of std::forward
:
/**
* @brief Forward an lvalue.
* @return The parameter cast to the specified type.
*
* This function is used to implement "perfect forwarding".
*/
template<typename _Tp>
constexpr _Tp&&
forward(typename std::remove_reference<_Tp>::type& __t) noexcept
{ return static_cast<_Tp&&>(__t); }
/**
* @brief Forward an rvalue.
* @return The parameter cast to the specified type.
*
* This function is used to implement "perfect forwarding".
*/
template<typename _Tp>
constexpr _Tp&&
forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
{
static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument"
" substituting _Tp is an lvalue reference type");
return static_cast<_Tp&&>(__t);
}
/**
* @brief Convert a value to an rvalue.
* @param __t A thing of arbitrary type.
* @return The parameter cast to an rvalue-reference to allow moving it.
*/
template<typename _Tp>
constexpr typename std::remove_reference<_Tp>::type&&
move(_Tp&& __t) noexcept
{ return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
“std: :forward is just syntactic sugar”? Is that true?
Depends on what one means by "is just syntactic sugar".
How do you comprehend “std: :forward is just syntactic sugar”?
I think that a correct and similar way to describe std::forward
is that it is a very simple function template that can be implemented in standard C++.
In other words, providing std::forward
in the standard library is not necessary in order for the programmer to do perfect forwarding.