Maybe overkill, but it would be possible here to use if constexpr
so the error case becomes a compile time error, like so:
0 std::string LogMsg(const std::string& msg)
1 {
2 const auto end_time = GetTime<std::chrono::microseconds>() - m_start_t;
3 if (m_start_t.count() <= 0) {
4 return strprintf("%s: %s", m_prefix, msg);
5 }
6
7 if constexpr (std::is_same<TimeType, std::chrono::microseconds>::value) {
8 return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
9 } else if constexpr (std::is_same<TimeType, std::chrono::milliseconds>::value) {
10 return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001);
11 } else if constexpr (std::is_same<TimeType, std::chrono::seconds>::value) {
12 return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001);
13 } else {
14 static_assert(dependent_false<TimeType>, "Error: unexpected time type");
15 }
16 }
for the static_assert
to work though this needs a utility function dependent_false
, which could in future be useful in other cases too (taken from p1830r1):
0template <bool value, typename... Args>
1inline constexpr bool dependent_bool_value = value;
2
3template <typename... Args>
4inline constexpr bool dependent_false = dependent_bool_value<false, Args...>;