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 void f(T&&)'
7:13: note: 'template 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.
The usual way is to provide appropriate overloads:
#include <iostream>
template <class T> void f(T&&) {
std::cout << "T&&\n";
}
template <class T> void f(T*) {
std::cout << "T*\n";
}
template <class T> void f(T&) {
std::cout << "T&\n";
}
int main() {
int i;
f(std::move(i));
f(&i);
f(i);
}
This produces the following output:
[temp]$ clang++ -std=c++11 test.cpp
[temp]$ ./a.out
T&&
T*
T&
[temp]$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With