Puppy
Puppy

Reputation: 146910

Overloading with rvalue references

Perfect forwarding is great and all. But what do I do if I want to not perfectly forward and instead just overload, on something that happens to be a templated type?

template<typename T> void foo(T&& ref);
template<typename T> void foo(const T& ref); 

Won't work because the first overload will invoke perfect forwarding. I'd really like behaviour very similar to if the first was a normal lvalue reference- where T will always be a value type.

Upvotes: 1

Views: 202

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 218700

#include <type_traits>

template<typename T>
typename std::enable_if
<
    !std::is_reference<T>::value,
    void
>::type
foo(T&& ref) {}

template<typename T> void foo(const T& ref) {}

Upvotes: 3

Related Questions