我正在观看最新的C9 讲座,并注意到一些有趣的事情。
在他对 type_traits 的介绍中,Stephan 使用了以下(如他所说,人为的)示例:
template <typename T>
void foo(T t, true_type)
{
std::cout << t << " is integral";
}
template <typename T>
void foo(T t, false_type)
{
std::cout << t << " is not integral";
}
template <typename T>
void bar(T t)
{
foo(t, typename is_integral<T>::type()
);
}
这似乎远比:
template <typename T>
void foo(T t)
{
if(std::is_integral<T>::value
)
std::cout << "integral";
else
std::cout << "not integral";
}
后一种方式有问题吗?他的方式更好吗?为什么?
谢谢。