Using the first approach, you can implement static-dispatch without using if/elseor switch.
template <typename T>
void Dispatch(T t)
{
foo(t, typename is_integral<T>::type());
}
Using the second approach, you must implement this with a block if/elseor switch,
template <typename T>
void Dispatch(T t)
{
if(std::is_integral<T>::value)
foo(t, true_type());
else
foo(t, false_type());
}
But if you want to implement your Dispatch()function without using if/else, and at the same time you want to use std::is_integral<T>::value, then you need to re-write your function foo(), for example,
template <bool b>
void foo(T t)
{
std::cout << t << " is integral";
}
template <>
void foo<false>(T t)
{
std::cout << t << " is not integral";
}
And your function Dispatch()will look like
template <typename T>
void Dispatch(T t)
{
const bool selector = (bool) std::is_integral<T>::value;
foo<selector>(t);
}
source
share