I haven arbitrary type T
that specializes std::numeric_limits
. Typically, for standard types that have the concept of a zero-element, std::numeric_limits<T>::zero()
would make sense, but as we know, std::numeric_limits
doesn’t provide such a member function.
My current workaround is using:
template<typename T>
constexpr T zero() {
return std::numeric_limits<T>::max() - std::numeric_limits<T>::max();
}
This seems to work because max() - max()
should return the zero-element of T
, but it feels like a hack and may not be the most clear approach.
Is there a more idiomatic or standardized way to infer a zero value for an arbitrary type T
that specializes std::numeric_limits
? Ideally, I’m looking for something that is both clear in intent and efficient. Any insights or alternative approaches would be greatly appreciated!