5

Given

template<class T>
struct TimeData
{
  T duration;
}

How can I write a static_assert to check in compile time if T is some type of std::chrono::duration? Remember std::chrono::duration is a template class.

In other words, how can I check if a type is some instantiation of a template class?

ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57

1 Answers1

16

You can implement your own type trait to check for a chrono::duration. Something like this:

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 {};

And then you can:

static_assert(is_duration<T>::value, "must be duration");
DeiDei
  • 10,205
  • 6
  • 55
  • 80