C++11 计时器环绕问题

C++11 计时器环绕问题,c++11,integer-overflow,chrono,C++11,Integer Overflow,Chrono,下面的代码正确吗 添加事件: 事件循环中: /。。。 自动现在=标准::计时::稳定时钟::现在(); 如果(event.expireTime,您可以使用稳定时钟::时间点::max().time\u-after\u-epoch()检查稳定时钟::时间点的范围。这将返回时钟::持续时间这是自稳定时钟epoch以来的最大时间量。epoch本身未指定。但是: auto time_left = steady_clock::time_point::max() - steady_clock::now();

下面的代码正确吗

添加事件: 事件循环中:
/。。。
自动现在=标准::计时::稳定时钟::现在();

如果(event.expireTime,您可以使用
稳定时钟::时间点::max().time\u-after\u-epoch()
检查
稳定时钟::时间点
的范围。这将返回
时钟::持续时间
这是自
稳定时钟
epoch以来的最大时间量。epoch本身未指定。但是:

auto time_left = steady_clock::time_point::max() - steady_clock::now();
给出到达
max()
之前的剩余时间。可以使用以下方法检查
剩余时间的确切单位:

using D = decltype(time_left);
std::cout << D::period::num << '/' << D::period::den << '\n';
这意味着单位是纳秒。所以你可以:

std::cout << time_left.count() << "ns\n";

这大约是292年。如果你的代码在我的平台上运行,它可能会在292年内结束。你的平台可能是相似的,这就是你可以测试它的方式。

所以答案是,不,但没关系,因为在所有主要平台上,持续时间都足够长。不要相信我的话。使用上面描述的工具来形成你自己的(最新消息)结论。还有
time\u point::max()
以及其他一切都是编译时信息,所以你可以
static\u断言它。根据我的计算,它实际上是29.2年。
2^63/(10^9*86400*365.2425)=292.277…
using D = decltype(time_left);
std::cout << D::period::num << '/' << D::period::den << '\n';
1/1000000000
std::cout << time_left.count() << "ns\n";
using date::operator<<;
std::cout << time_left << "\n";
9221890448824928278ns