I believe that specialization is the wrong tool here. Instead, consider making the target type an additional type parameter.
// C++14
template <typename OutputT, typename InputT>
std::enable_if_t
<
std::is_arithmetic<InputT>::value && std::is_floating_point<OutputT>::value,
OutputT
>
hz_to_nsec(const InputT freq)
{
return (freq != InputT {0})
? static_cast<OutputT>(NSEC_PER_SEC) / static_cast<OutputT>(freq)
: OutputT {0};
}
I'm putting OutputT
first because it cannot be deduced. I have also restricted the permissible types for InputT
and OutputT
to what might be sensible types.
It can be used like this.
hz_to_nsec<double>(10); // InputT = int, OutputT = double
hz_to_nsec<float>(10.0f): // InputT = float, OutputT = float
hz_to_nsec<float>(5UL); // InputT = unsigned long, OutputT = float