Maybe overkill, but it would be possible here to use if constexpr so the error case becomes a compile time error, like so:
std::string LogMsg(const std::string& msg)
{
const auto end_time = GetTime<std::chrono::microseconds>() - m_start_t;
if (m_start_t.count() <= 0) {
return strprintf("%s: %s", m_prefix, msg);
}
if constexpr (std::is_same<TimeType, std::chrono::microseconds>::value) {
return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
} else if constexpr (std::is_same<TimeType, std::chrono::milliseconds>::value) {
return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001);
} else if constexpr (std::is_same<TimeType, std::chrono::seconds>::value) {
return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001);
} else {
static_assert(dependent_false<TimeType>, "Error: unexpected time type");
}
}
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):
template <bool value, typename... Args>
inline constexpr bool dependent_bool_value = value;
template <typename... Args>
inline constexpr bool dependent_false = dependent_bool_value<false, Args...>;