std::chrono::treat_as_floating_point (3) - Linux Manuals
std::chrono::treat_as_floating_point: std::chrono::treat_as_floating_point
Command to display std::chrono::treat_as_floating_point
manual in Linux: $ man 3 std::chrono::treat_as_floating_point
NAME
std::chrono::treat_as_floating_point - std::chrono::treat_as_floating_point
Synopsis
Defined in header <chrono>
template <class Rep> (since C++11)
struct treat_as_floating_point : std::is_floating_point<Rep> {};
The std::chrono::treat_as_floating_point trait helps determine if a duration can be converted to another duration with a different tick period.
Implicit conversions between two durations normally depends on the tick period of the durations. However, implicit conversions can happen regardless of tick period if std::chrono::treat_as_floating_point<Rep>::value == true.
Helper variable template
template< class Rep > (since C++17)
inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<Rep>::value;
Example
// Run this code
#include <iostream>
#include <chrono>
#include <thread>
void timed_piece_of_code()
{
std::chrono::milliseconds simulated_work(2);
std::this_thread::sleep_for(simulated_work);
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
std::cout << "Running some timed piece of code..." << '\n';
timed_piece_of_code();
auto stop = std::chrono::high_resolution_clock::now();
// A floating point milliseconds type
using FpMilliseconds =
std::chrono::duration<float, std::chrono::milliseconds::period>;
static_assert(std::chrono::treat_as_floating_point<FpMilliseconds::rep>::value,
"Rep required to be floating point");
// Note that implicit conversion is not allowed here
auto i_ms = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
// Note that implicit conversion is allowed here
auto f_ms = FpMilliseconds(stop - start);
std::cout << "Time in milliseconds, using default rep: "
<< i_ms.count() << '\n';
std::cout << "Time in milliseconds, using floating point rep: "
<< f_ms.count() << '\n';
}
Possible output:
Running some timed piece of code...
Timing stats:
Time in milliseconds, using default rep: 2
Time in milliseconds, using floating point rep: 2.57307
Pages related to std::chrono::treat_as_floating_point
- std::chrono::tai_clock (3) - std::chrono::tai_clock
- std::chrono::tai_clock::from_utc (3) - std::chrono::tai_clock::from_utc
- std::chrono::tai_clock::now (3) - std::chrono::tai_clock::now
- std::chrono::tai_clock::to_utc (3) - std::chrono::tai_clock::to_utc
- std::chrono::time_of_day (3) - std::chrono::time_of_day
- std::chrono::time_of_day<Duration>::hours,std::chrono::time_of_day<Duration>::minutes, (3) - std::chrono::time_of_day<Duration>::hours,std::chrono::time_of_day<Duration>::minutes,
- std::chrono::time_of_day<Duration>::make12,std::chrono::time_of_day<Duration>::make24 (3) - std::chrono::time_of_day<Duration>::make12,std::chrono::time_of_day<Duration>::make24
- std::chrono::time_of_day<Duration>::operatorprecision, (3) - std::chrono::time_of_day<Duration>::operatorprecision,
- std::chrono::time_of_day<Duration>::operatorprecision,std::chrono::time_of_day<Duration>::to_duration (3) - std::chrono::time_of_day<Duration>::operatorprecision,std::chrono::time_of_day<Duration>::to_duration
- std::chrono::time_of_day<Duration>::time_of_day (3) - std::chrono::time_of_day<Duration>::time_of_day
- std::chrono::time_point (3) - std::chrono::time_point
- std::chrono::time_point<Clock,Duration>::max (3) - std::chrono::time_point<Clock,Duration>::max
- std::chrono::time_point<Clock,Duration>::min (3) - std::chrono::time_point<Clock,Duration>::min
- std::chrono::time_point<Clock,Duration>::operator++, (3) - std::chrono::time_point<Clock,Duration>::operator++,
- std::chrono::time_point<Clock,Duration>::operator++,std::chrono::time_point<Clock,Duration>::operator-- (3) - - std::chrono::time_point<Clock,Duration>::operator++,std::chrono::time_point<Clock,Duration>::operator--
- std::chrono::time_point<Clock,Duration>::operator+,operator- (3) - - std::chrono::time_point<Clock,Duration>::operator+,operator-
- std::chrono::time_point<Clock,Duration>::time_point (3) - std::chrono::time_point<Clock,Duration>::time_point
- std::chrono::time_point<Clock,Duration>::time_since_epoch (3) - std::chrono::time_point<Clock,Duration>::time_since_epoch