Skip to content

C++ std::forward sample implementation

This is fun sample of a possible implementation of C++ STL’s std::forward()

// this code is how std::forward actually works.
template<typename T>
void foo(T&& t){
    if constexpr (std::is_lvalue_reference<T>::value){
        std::cout << "LVALUE reference\n";
        func(t);
    }else{
        std::cout << "RVALUE reference\n";
        func(std::move(t));
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *