为什么numeric_limits对引用类型不起作用? [英] Is there a reason why numeric_limits do not work on reference types?
问题描述
如果您错误地执行了以下操作:
If you mistakenly do something like:
#include<limits>
int arr[3];
auto x = std::numeric_limits<decltype(arr[0])>::max();
您将从STL实现中的文件中获得无用的错误消息.
You will get unhelpful error message from the file in the STL implementation.
问题在于模板参数是一个引用,因此解决方法是将其删除:
Problem is that template argument is a reference, so the fix is to remove it:
auto x = std::numeric_limits<std::remove_reference_t<decltype(arr[0])>>::max();
现在我的问题是为什么 numeric_limits
不知道自己这样做?我会理解您不希望删除指针性(因为 char
指针的 max
和 char
的 max
完全不同),但是我认为只要您将引用作为 numeric_limits
的参数,您都会对删除它所获得的结果感到满意.
Now my question is why numeric_limits
do not know to do this by themselves?
I would understand that you do not want to remove pointerness(since max
of char
pointer and max
of char
are very very different things), but I would assume that whenever you have a reference as an argument to numeric_limits
you would be happy with result that is obtained by removing it.
推荐答案
从技术角度来看,没有理由说明 std :: numeric_limits< T>
不能与引用一起使用.添加像这样的部分专业化所需的一切:
From a technical point of view, there is no reason why std::numeric_limits<T>
couldn't work with references. All what would be needed it to add a partial specialisations like this:
namespace std {
template <typename T> struct numeric_limits<T&>: numeric_limits<T> {};
template <typename T> struct numeric_limits<T&&>: numeric_limits<T> {};
template <typename T> struct numeric_limits<T const>: numeric_limits<T> {};
template <typename T> struct numeric_limits<T volatile>: numeric_limits<T> {};
template <typename T> struct numeric_limits<T const volatile>: numeric_limits<T> {};
}
用户当然不能添加这些专业.但是,这不是一个很大的限制,因为可以在适当的命名空间中创建 numeric_limits
的自定义变体.
A user can't add these specialisations, of course. However, that's not a huge constraint as a custom variant of numeric_limits
can be created in a suitable namespace.
由于从技术上讲可行,现在的问题是为什么该标准不提供这些声明.我认为不会有一个结论性的答案(除非对该想法进行了讨论并以合适且仍可访问的记录进行了丢弃).以下是一些可能的答案:
As it is technically doable the question now becomes why the standard doesn't provide these declarations. I don't think there will be a conclusive answer (unless this idea was discussed and discarded with a suitable and still accessible record). Here are some of the potential answers:
- 未提出该功能.当引入
std :: numeric_limits
时,它专门针对于使用更多的C ++方法替换< limits.h>
中的宏.诸如decltype(expr)
之类的东西和转发引用都不存在,即模板参数不会被偶然"推导为引用类型.因此,当时不考虑删除预选赛. - 我不确定是否在历史上添加
numeric_limits
时已经存在部分模板专门化功能.即使它存在,也不存在类似于模板元编程的任何东西.结果,可能不可能或假定不可能以必要的方式来插入模板参数类型. - 即使考虑到这一点,我也怀疑委员会是否会添加部分专业知识:
numeric_limits< T>
会检查T
类型的特征,但引用类型不会没有max()
或digits
.另外,如果支持引用类型是因为显然"所需的属性必须是要停止的基础类型之一:应该std :: numeric_limits< int *> :: max()
提供以下内容:与std :: numeric_limits< int> :: max()
的值也一样吗?毕竟,它对指针也没有任何意义. - 考虑到原始提案几乎肯定没有涵盖限定类型的情况(请参见上文),因此该功能不可用的另一个原因是根本没有提出该提案:没有提案,标准就不会换衣服.如果提出该功能,是否会更改标准是一个单独的问题.在此常规空间中有一个建议( P0437r0 ),但浏览它,我认为该建议不包括合格的建议类型.
- The feature wasn't proposed. When
std::numeric_limits
was introduced it specifically targeted replacing the macros in<limits.h>
with a more a C++ approach. Something likedecltype(expr)
and forwarding references didn't exist, i.e., template arguments wouldn't be "accidentally" deduced as reference types. Thus, removing qualifiers wasn't a concern at the time. - I'm not sure if at the point in history when
numeric_limits
were added partial template specialisation already existed. Even if it existed, anything resembling template meta programming didn't exist. As a result, it may not have been possible or assumed to be possible to meddle with the template argument type in the necessary way. - Even if it were considered, I doubt the committee would have gone with adding the partial specialisations:
numeric_limits<T>
inspects the traits of typeT
but reference types don't have amax()
ordigits
. Also, if reference types are supported because "clearly" the desired property must be the one of the underlying type where to stop: shouldstd::numeric_limits<int*>::max()
provide the same value asstd::numeric_limits<int>::max()
, too? After all, it also doesn't make any sense on pointers. - Considering that the original proposal almost certainly didn't cover the case of qualified types (see above), another reason why the feature isn't available is that it simply wasn't proposed: without a proposal the standard won't get changed. Whether the standard would get changed if the feature were proposed is a separate question. There is a proposal in this general space (P0437r0) but browsing over it I don't think this proposal covers qualified types, either.
这篇关于为什么numeric_limits对引用类型不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!