C ++ 11 <chrono> overflow guarantees
I have this piece of code:
auto time_point_a = std::chrono::high_resolution_clock::now();
while (true) {
auto time_point_a = std::chrono::high_resolution_clock::now();
auto counter_ms = std::chrono::duration_cast<std::chromo::milliseconds(time_point_a - time_point_b);
// more code
std::count << counter_ms.count() << std::endl;
}
Is it mandatory for counter_ms.count () to always return a valid value? Is there any chance that count () throws away? What happens if counter_ms exceeds the size of its main integral type (I believe it is long)? My program will run for several days in a row, and I need to know what happens if / when counter_ms gets too big.
+4
1 answer
Is it mandatory for counter_ms.count () to always return a valid value?
counter_ms . - .count() , , .
, count() ?
- noexcept :
noexceptstd:: lib.- , , .
counter_ms , , , .
, .
, counter_ms ( , )?
:
#include <chrono>
#include <iostream>
#include "type_name.h"
int
main()
{
std::cout << type_name<std::chrono::milliseconds::rep>() << '\n';
}
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>, (lib++). , , , 45- , 64- .
, , ( undefined).
+7