我有这段代码:
auto time_point_a = std::chrono::high_resolution_clock::now();
while (true) {
auto time_point_b = std::chrono::high_resolution_clock::now();
auto counter_ms = std::chrono::duration_cast<std::chromo::milliseconds(time_point_b - time_point_a);
// more code
std::cont << counter_ms.count() << std::endl;
}
counter_ms.count()
是否保证始终返回有效值? count()
有可能抛出吗?如果 counter_ms 超过其基础整数类型的大小(我认为它是 long long
),会发生什么情况?我的程序将连续运行几天,我需要知道如果/当 counter_ms
变得太大时会发生什么。
最佳答案
Is counter_ms.count() guaranteed to always return a valid value?
counter_ms
持有一个带符号的整数毫秒数。 .count()
指定成员函数只返回这个有符号整数值。
Is there any chance that count() throws?
这个成员函数没有标记noexcept
有两个原因:
-
noexcept
在 std::lib 中很少使用。 - 通常,持续时间允许基于算术模拟器,它可能具有抛出复制构造函数。
在counter_ms
的情况下,表示必须是带符号的整数类型,当然不能抛出复制构造。
这不可能抛出。
What happens if counter_ms exceeds the size of its underlying integral type (I reckon it's long long)?
您可以使用此程序检查底层整数类型:
#include <chrono>
#include <iostream>
#include "type_name.h"
int
main()
{
std::cout << type_name<std::chrono::milliseconds::rep>() << '\n';
}
描述“type_name.h”的地方here .对我来说,这个程序输出:
long long
标准规范规定此类型必须是至少 45 位的带符号整数类型。这使它的范围至少为 +/- 557 年。您可以找到您实现的实际范围 milliseconds
使用此程序:
#include <chrono>
#include <iostream>
int
main()
{
using days = std::chrono::duration
<int, std::ratio_multiply<std::ratio<24>, std::chrono::hours::period>>;
using years = std::chrono::duration
<int, std::ratio_multiply<std::ratio<146097, 400>, days::period>>;
std::cout << std::chrono::duration_cast<years>
(std::chrono::milliseconds::min()).count() << " years\n";
std::cout << std::chrono::duration_cast<years>
(std::chrono::milliseconds::max()).count() << " years\n";
}
对我来说输出:
-292277024 years
292277024 years
巧合的是,我是实现 <chrono>
的人我正在使用的实现(libc++)。实际范围比要求的最小范围大得多的原因是我在定位 45 位有符号整数类型时遇到了问题,不得不接受 64 位有符号整数类型。
超出此范围时,您将得到与带符号整数算术溢出完全相同的行为(指定为未定义行为)。
关于c++ - <chrono> 溢出保证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35222170/