c++ - 是否保证 std::chrono::steady_clock 永远不会环绕?

标签 c++ c++11 chrono

基本上:

auto p1 = std::steady_clock::now().time_since_epoch().count();

... // do smth for unspecified period of time

auto p2 = std::steady_clock::now().time_since_epoch().count();

assert(p2 >= p1);   // is this guaranteed?
?
如果是 - 这是如何保证的?通过确保没有 C++ 程序运行时间超过 std::steady_clock::duration::max() - std::steady_clock::duration::zero() ?

最佳答案

像这个主题领域的许多事情一样,它很复杂......
是的,保证steady_clock::now()从不换行,因为它保证它总是增加:
http://eel.is/c++draft/time.clock.steady

Objects of class steady_­clock represent clocks for which values of time_­point never decrease as physical time advances and for which values of time_­point advance at a steady rate relative to real time. That is, the clock may not be adjusted.


但...

how it this guaranteed


从技术上讲,不可能永远拥有有限内存滴答的时钟并且永远不会超出范围。所以从技术上讲,答案是否定的。
但 ...
作为一种实用的方式,steady_clock 的所有实现计数 nanoseconds使用有符号的 64 位整数类型。范围是 +/-292 年。 epoch 通常是您的计算机最后一次启动。所以实际上,它不会在你的一生中结束。因此,当它结束时,这将是其他人的问题,并且所涉及的计算机将获得某种奖励,因为它具有如此长的正常运行时间。
PS:我等不及电脑手册出来了,上面写着:

Must be rebooted at least once every 292 years.


:-)

关于c++ - 是否保证 std::chrono::steady_clock 永远不会环绕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64252801/

相关文章:

c++ - 用内存地址声明函数

c++ - 使用纯抽象数据类型作为返回?

c++ - 条件类型特征通用引用的问题

C++ 中的正则表达式转义

c++ - std::chrono::years 存储真的至少是 17 位吗?

c++ - 为什么 unordered_map 的 emplace with piecewise_construct 参数需要默认构造函数?

c++ - 在opencv中检测人脸何时进入ROI

c++ - 通过opencv中的选定像素裁剪图像

c++ - 将此时间字符串解析为 'std::chrono' 有什么问题?