If you want non-integer types to cause a compilation error, you can also assert statically (compile-time assertions).
With C++0x:
#include <utility>
template <class T>
void foo(T )
{
static_assert(std::is_integral<T>::value, "Only integral types allowed");
}
int main()
{
foo(3); //OK
foo(3.14); //makes assertion fail
}
With C++03, boost will help:
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>
template <class T>
void foo(T )
{
BOOST_STATIC_ASSERT(boost::is_integral<T>::value);
}
int main()
{
foo(3);
foo(3.14);
}
(IMO, enable_if
is for scenarios where you want to enable some other version of the function for other types and avoid getting an error. If you want an error for all other types, having the function disabled, you might just get a not too helpful message: "no matching function to call", which doesn't even point to the place in the code where non-integers are disallowed.)