C++ STL type_traits question.

Posted by Kim Sun-wu on Stack Overflow See other posts from Stack Overflow or by Kim Sun-wu
Published on 2010-12-30T07:48:18Z Indexed on 2010/12/30 7:54 UTC
Read the original article Hit count: 255

Filed under:
|
|
|

I was watching the latest C9 lecture and noticed something interesting..

In his introduction to type_traits, Stephan uses the following (as he says, contrived) example:

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()); }

This seems to be far more complicated than:

template <typename T> void foo(T t)
{
    if(std::is_integral<T>::value)
        std::cout << "integral";
    else
        std::cout << "not integral";
}

Is there something wrong with the latter way of doing it? Is his way better? Why?

Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates