A naive implementation of this template is dangerous, because the call site might accidentally omit the template parameter:
0template <typename D>
1D GetRandDur(const D& duration_max)
2{
3 return D{GetRand(duration_max.count())};
4}
5
6BOOST_AUTO_TEST_CASE(util_time_GetRandTime)
7{
8 std::chrono::seconds rand_hour = GetRandDur(std::chrono::hours{1});
9 // Want seconds to be in range [0..1hour), but always get zero :((((
10 BOOST_CHECK_EQUAL(rand_hour.count(), 0);
11}
Luckily std::common_type
is already specialised in the standard lib for std::chrono::duration
(https://en.cppreference.com/w/cpp/chrono/duration/common_type). And its effect seem to be that the call site must always specify the template argument explicitly.
So instead of implementing the function for each duration type by hand, replace it with a templated version that is safe to use.