Question

I am still learning TMP and using <type_traits>. I was looking at Microsoft's implementation of std::chrono::duration_cast<T> and I am hoping someone can help me understand how their _Is_duration class template works. I appreciate the help.

template<class _Ty>
    struct _Is_duration
    {   // tests for duration
    static const bool value = false;
    };

// duration_cast
template<class _To,
    class _Rep,
    class _Period> inline
    typename enable_if<_Is_duration<_To>::value, _To>::type
        duration_cast(const duration<_Rep, _Period>& _Dur)
{
...
}
Was it helpful?

Solution

That's not the full implementation. It's probably going to be partially specialized for durations, something like:

template<class _Rep,
    class _Period>
    struct _Is_duration<duration<_Rep, _Period>>
    {   // tests for duration
    static const bool value = true;
    };

Thus, if you pass a duration as the type parameter, the partial specialization will be used and value == true, and otherwise the base template will be used, and value == false.

OTHER TIPS

It's probably just specialized for durations somewhere else in the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top