`enable_if` Contains Template Pack

- June 1, 2022

namespace serverlog::detail
{
    // is_duration checks if a type is std::chrono::duration
    template <class T>
    struct is_duration : std::false_type
    {
    };

    template <class Rep, class Period>
    struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type
    {
    };

    // "contains_duration" checks if a variadic template contains std::chrono::duration type

    // handles empty parameter pack case, ie: contains_duration<>
    template <class...>
    struct contains_duration : std::false_type
    {
    };

    // picks the first parameter T, and checks if it is of duration type, after stripping it for const/reference types
    template <class T, class... TT>
    struct contains_duration<T, TT...> : is_duration<std::decay_t<T>>
    {
    };

    // recursive case picking one-by-one until the pack is exhausted
    template <class T, class U, class... TT>
    struct contains_duration<T, U, TT...> : std::bool_constant<contains_duration<T>() || contains_duration<U, TT...>{}>
    {
    };

}; // namespace serverlog::detail

bais

See Also

Comments

Any comments? Create a new discussion on GitHub.
There used to be an inline comment form here, but it was removed.