Menu

Everything about `std::forward`
📰

Everything about `std::forward`

DEV Community: cpp·llzzz·14 days ago
#KTH1lMbu
#devtocpp#devto#dev#forward#reference
Reading 0:00
15s threshold

std::forward is similar to std::move . It is just a static_cast to rvalue reference . But it is slightly different than std::move , std::forward is a conditional cast that only cast the type to rvalue-reference if some condition is satisify. Let dive deeper in it!

First of all, let look at a custom implementation of std::forward , this is not the exact definition in the STL, but it is close enough.

namespace std { template < typename T > __attribute__ (( always_inline )) constexpr T && forward ( std :: remove_reference_t < T >& t ) noexcept { return static_cast < T &&> ( t ); }

template < typename T > __attribute__ (( always_inline )) constexpr T && forward ( std :: remove_reference_t < T >&& t ) noexcept { static_assert ( !…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.