mercredi 28 novembre 2018

Select function based on if template is pointer/reference or none

I would like to provide different implementations of a function dependant on if it is a pointer, a reference or a regular type. This is my code so far:

template<class T,
         class = typename std::enable_if_t<std::is_reference<T>::value>>
void f(T && in)
{}

// This causes redefinition error 
template<class T,
    class = typename std::enable_if_t<std::is_pointer<T>::value>>
void f(T && in)
{}

template<class T,
    class = typename std::enable_if_t<!std::is_reference<T>::value>,
    class = typename std::enable_if_t<!std::is_pointer<T>::value>>
void f(T && in)
{}

The middle function causes:

12:13: error: redefinition of 'template<class T, class> void f(T&&)' 7:13: note: 'template<class T, class> void f(T&&)' previously declared here

Funnily only the first and last function together compile.

Any ideas how to fix it or simplify this code.

Aucun commentaire:

Enregistrer un commentaire