为什么std :: numeric_limits< seconds> :: max()返回0? [英] Why does std::numeric_limits<seconds>::max() return 0?

查看:243
本文介绍了为什么std :: numeric_limits< seconds> :: max()返回0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个有趣的问题与 std :: numeric_limits< seconds> :: max()返回0.答案是使用秒: :max() std :: numeric_limits< seconds :: rep> :: max(),但我有兴趣知道为什么发生。我希望它在编译时失败或只是工作。下面的代码演示了gcc 4.9.3的问题。

  #include< iostream> 
#include< limits>
#include< chrono>

using namespace std;
using namespace std :: chrono;

int main(int / * argc * /,const char * / * argv * / [])
{
const auto maxSeconds = std :: numeric_limits< seconds> ;: :max();
std :: cerr<< maxSeconds.count()<< \\\
;
const auto maxSeconds2 = seconds :: max();
std :: cerr<< maxSeconds2.count()< \\\
;
return 0;
}

我在 chrono 头文件。如果 duration 隐式转换为数字类型,并且符号丢失或 bool






由于TartanLlama指出,默认的特殊化使用了默认构造函数,因此返回0。



调试标准我看到以下内容:


18.3.2.3类模板 numeric_limits [numeric.limits]



非算术标准类型,例如 complex< ;


稍后:

/ p>


默认的 numeric_limits< T> 模板应包含所有成员,

cv限定类型<$ c的专业化 numeric_limits 的每个成员的值$ c> cv T 将等于
的值对应于非限定类型的专业化成员
T


缺少的是解释为什么委员会认为这是一个比编译失败更好的主意。






更新:我提出这是ISO委员会的问题



https://issues.isocpp.org/show_bug.cgi?id = 186

解决方案

std :: numeric_limits 专门为 std :: chrono :: seconds 。为 std :: numeric_limits 中的所有数据成员和函数提供默认定义,以避免未指定类型的编译器错误。 numeric_limits< T> :: max()的默认版本只返回 T() $ c> 0



您可以检查 std :: numeric_limits 通过检查 std :: numeric_limits< T> :: is_specialized ,在编译时为给定的 T 默认为 false


I found an interesting gotcha with std::numeric_limits<seconds>::max() returning 0. The answer is to use seconds::max() or std::numeric_limits<seconds::rep>::max() instead, but I am interested to know why this happens. I would expect it to either fail at compile time or just work. The following code demonstrates the issue with gcc 4.9.3.

#include <iostream>
#include <limits>
#include <chrono> 

using namespace std;
using namespace std::chrono;

int main(int /*argc*/, const char* /*argv*/[])
{
    const auto maxSeconds = std::numeric_limits<seconds>::max();
    std::cerr << maxSeconds.count() << "\n";
    const auto maxSeconds2 = seconds::max();
    std::cerr << maxSeconds2.count() << "\n";
   return 0;
}

I can't see any implicit conversions in the chrono header file. If a duration had implicitly cast to a numeric type and the sign was lost or a bool you could end up with a minimum of zero - but a maximum of zero doesn't make sense.


As TartanLlama points out the default specialization uses the default constructor and therefore returns 0.

Delving into an old copy of the standard I see the following dictats:

18.3.2.3 Class template numeric_limits [numeric.limits]

Non-arithmetic standard types, such as complex<T> (26.4.2), shall not have specializations.

and a little later:

The default numeric_limits<T> template shall have all members, but with 0 or false values.

The value of each member of a specialization of numeric_limits on a cv-qualified type cv T shall be equal to the value of the corresponding member of the specialization on the unqualified type T.

What is missing is an explanation of why this was considered a better idea by the committee than a compilation failure. Is a library defect report warranted?


Update: I have raised this as an issue with the ISO committee

https://issues.isocpp.org/show_bug.cgi?id=186

解决方案

std::numeric_limits is not specialized for std::chrono::seconds. Default definitions are given for all data members and functions in std::numeric_limits to avoid compiler errors for unspecialized types. The default version of numeric_limits<T>::max() simply returns T(), which is 0 in this case.

You can check if std::numeric_limits is specialized for a given T at compile time by checking std::numeric_limits<T>::is_specialized, which defaults to false.

这篇关于为什么std :: numeric_limits&lt; seconds&gt; :: max()返回0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆