Search code examples
c++c++11c++14rvaluelvalue

forwarding reference with containers


It's possible that give std::vector<T> e.g to function by forwarding reference? I know that

template<typename T>
void f(const std::vector<T>&&){} // is rvalue
template<typename T>
void f(const std::vector<T>&){} // is lvalue

but how I can make function for lvalue and rvalue (with std::vector as parametr)?


Solution

  • I think you want

    // traits for detecting std::vector
    template <typename> struct is_std_vector : std::false_type {};
    template <typename T, typename A>
    struct is_std_vector<std::vector<T, A>> : std::true_type {};
    
    template<typename T>
    std::enable_if_t<is_std_vector<std::decay_t<T>>::value>
    f(T&&); // Take any std::vector