基本上:
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 oftime_point
never decrease as physical time advances and for which values oftime_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/