Question: Suppose I have a template function:

```
template<typename T>
void f(T t)
{
    ...
}

```

and I want to write a specialization for all primitive integer types. What is the best way to do this?

What I mean is:

```
template<typename I where is_integral<I>::value is true>
void f(I i)
{
    ...
}

```

and the compiler selects the second version for integer types, and the first version for everything else?

Answer: 
Using c++11, std::enable\_if ( <http://en.cppreference.com/w/cpp/types/enable_if> ) can be used to do that:

```
template<typename T, class = typename std::enable_if<std::is_integral<T>::value>::type>
void f(T t) {...}

```
You can use a helper template that you can specialize like this:

```
#include <string>
#include <iostream>
#include <type_traits>

template <typename T, bool = std::is_integral<T>::value>
struct Foo {
        static void bar(const T& t) { std::cout << "generic: " << t << "\n"; }
};
template <typename T>
struct Foo<T, true> {
        static void bar(const T& t) { std::cout << "integral: " << t << "\n"; }
};

template <typename T>
static void bar(const T& t) {
        return Foo<T>::bar(t);
}

int main() {
        std::string s = "string";
        bar(s);
        int i = 42;
        bar(i);
        return 0;
}

```

output:

```
generic: string
integral: 42

```