I'm developing a time utils library with C++11 so that my colleagues can use it.
They all have been using C-style time APIs for a long time and are not familiar with std::chrono in C++11. So I want to write some basic functions. For example, count ticks of std::chrono:
template<typename T = std::chrono::milliseconds, typename = typename std::enable_if<
std::is_same<std::chrono::nanoseconds, T>::value ||
std::is_same<std::chrono::microseconds, T>::value ||
std::is_same<std::chrono::milliseconds, T>::value ||
std::is_same<std::chrono::seconds, T>::value ||
std::is_same<std::chrono::minutes, T>::value ||
std::is_same<std::chrono::hours, T>::value>::type>
inline uint64_t getTimeTicksInMS(const T &t) {
return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
}
This function can count the number of time ticks with milliseconds. For example, getTimeTicksInMS(std::chrono::seconds{2}) will return 2000.
Now I want to make it more flexible.
template<typename V, typename T>
inline uint64_t getTimeTicks(const T &t) {
return std::chrono::duration_cast<V>(t).count();
}
Now, it can return the number of ticks with any kind of time.
However, as you see, I removed the std::enable_if part, because it would become very long as I have two types now: T and V.
Is there some better way to code std::enable_if in this case without very long std::enable_if?
Aucun commentaire:
Enregistrer un commentaire