Is there a better way of doing the following?
I have a vector class, with the following function:
template <typename T>
bool Vector3<T>::IsUnitVector() const
{
return IsAlmostEqual(this->GetLength(), One<T>::Value());
}
As T can be float or double (I'm using explicit template instantiation to make sure only these types are supported), I've had to create a helper class, which returns the value of 1, in the correct type:
template <typename T>
struct One
{
static T Value();
};
template <>
struct One<int>
{
static int Value() { return 1; }
};
template <>
struct One<float>
{
static float Value() { return 1.0f; }
};
template <>
struct One<double>
{
static double Value() { return 1.0; }
};
This wasn't too bad until I realised I need to create a Zero
class as well for other comparisons. So my question is, is there a better way of achieving this?
return IsAlmostEqual(this->GetLength(), static_cast<T>(1));
Small, nonnegative integer values should all be exactly representable by each of the numeric types, so simply static_cast
'ing to the desired type should be sufficient.
Alternatively, assuming IsAlmostEqual
is a static member function that has two parameters of type T
(e.g. as IsAlmostEqual(T lhs, T rhs)
), simply let the compiler perform the conversion automatically in the function call:
return IsAlmostEqual(this->GetLength(), 1);
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