I have a template function, let's say:
template <typename T>
void foo(T input) {
// some funny processing
}
I only want to enable this function for T == string or T == stringpiece. How do I do that using std::enable_if ???
I have a template function, let's say:
template <typename T>
void foo(T input) {
// some funny processing
}
I only want to enable this function for T == string or T == stringpiece. How do I do that using std::enable_if ???
You can just use overloading for this:
template<typename T>
void foo(T);
void foo(string str) { }
void foo(stringpiece sp) { }
You can use is_same
to check two types are the same and then use enable_if
in the return type of the function:
#include <string>
#include <type_traits>
#include <functional>
struct stringpiece {
};
template<typename T>
typename std::enable_if<std::is_same<std::string, T>::value || std::is_same<stringpiece, T>::value>::type
foo(T input) {
// Your stuff here
(void)input;
}
int main() {
foo(stringpiece());
foo(std::string(""));
}