【发布时间】:2011-06-01 10:45:56
【问题描述】:
我在看最新的C9 lecture,发现了一些有趣的东西..
在他对 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";
}
后一种方式有问题吗?他的方式更好吗?为什么?
谢谢。
【问题讨论】:
-
在 C++17 中我们有
if constexpr。
标签: c++ templates stl typetraits