I can define a constant as either a float or a 32-bit uint:
const float SecondsPerMinute = 60.0F;
or
const uint32 SecondsPerMinute = 60U;
The const is used in some equations that expect an int and some equations that expect a float. I want to make my compiler and static analysis tools happy so I will static_cast it to the appropriate type as necessary.
Is it better to define the const as a float and cast to an int, or define it as an int and cast it to a float? Does it make a difference, or is this more a matter of personal opinion?
Let's say the constant is used an equal number of times as an int and as a float.
How about a template:
template <typename T>
constexpr T SecondsPerMinute = T(60);
Usage:
std::printf("%d %f", SecondsPerMinute<int>, SecondsPerMinute<double>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With