How can I specify what is required to be a valid template argument? What I mean is let's for example take something like this:
template<class T>
void f(const T& obj)
{
//do something with obj
}
but I would like T to be only integer type so I would accept char, int, short unsigned etc but nothing else. Is there (I'm sure there is) a way to detect it what is provided as a template arg?
Thank you.
You can use boost::enable_if and boost::is_integral (also included in the TR1):
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>
template <typename T>
typename boost::enable_if<boost::is_integral<T> >::type
f(const T & obj)
{
...
}
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