[英]Min/max values of a templated type
I have a rather basic question.我有一个相当基本的问题。 Suppose I have a templated function:
假设我有一个模板化函数:
bool is_max(std::uint32_t val) {
return (val == std::numeric_limits<uint32_t>::max());
}
However, to make my function flexible, I'd like to use templates:但是,为了使我的功能灵活,我想使用模板:
template <typename T>
bool is_max(T val) {
return (val == ???);
}
The std::numeric_limits function would work with built-in types, but obviously doesn't work for user-defined types. std::numeric_limits 函数适用于内置类型,但显然不适用于用户定义的类型。
Is there some standard operator or function that the current built-in and most numeric types (I'm trying to use cnl::fixed_point ) that can be used here to find min/max value?当前的内置和大多数数字类型(我正在尝试使用cnl::fixed_point )是否有一些标准运算符或函数可以在此处用于查找最小值/最大值?
The way to handle this is to continue using std::numeric_limits
, and add template specializations for all of the desired input types.处理这个问题的方法是继续使用
std::numeric_limits
,并为所有所需的输入类型添加模板特化。
Quoting cppreference :引用cppreference :
Implementations may provide specializations of
std::numeric_limits
for implementation-specific types: eg GCC providesstd::numeric_limits<__int128>
.实施方式可以提供的特化
std::numeric_limits
为实现特定的类型:例如GCC提供std::numeric_limits<__int128>
Non-standard libraries may add specializations for library-provided types, eg OpenEXR providesstd::numeric_limits<half>
for a 16-bit floating-point type.非标准库可能会为库提供的类型添加特化,例如 OpenEXR 为 16 位浮点类型提供
std::numeric_limits<half>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.