std::chrono::duration_cast (3) - Linux Manuals
std::chrono::duration_cast: std::chrono::duration_cast
NAME
std::chrono::duration_cast - std::chrono::duration_cast
Synopsis
template <class ToDuration, class Rep, class Period> (since C++11)
constexpr ToDuration duration_cast(const duration<Rep,Period>& d);
Converts a std::chrono::duration to a duration of different type ToDuration.
No implicit conversions are used. Multiplications and divisions are avoided where possible, if it is known at compile time that one or more parameters are 1. Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished.
Parameters
d - duration to convert
Return value
d converted to a duration of type ToDuration.
Notes
The function does not participate in overload resolution unless ToDuration is an instance of std::chrono::duration.
Casting between integer durations where the source period is exactly divisible by the target period (e.g. hours to minutes) or between floating-point durations can be performed with ordinary casts or implicitly via std::chrono::duration_constructors, no duration_cast is needed.
Casting from a floating-point duration to an integer duration is subject_to_undefined_behavior when the floating-point value is NaN, infinity, or too large to be representable by the target's integer type. Otherwise, casting to an integer duration is subject to truncation as with any static_cast to an integer type.
Example
This example measures the execution time of a function
// Run this code
Possible output:
See also
time_point_cast (function template)
floor(std::chrono::duration) converts a duration to another, rounding down
(C++17)
ceil(std::chrono::duration) converts a duration to another, rounding up
(C++17)
round(std::chrono::duration) converts a duration to another, rounding to nearest, ties to even
(C++17)