std::chrono...on::duration(3)
π std::chrono...on::duration(3) π
okay so this is the page for std::chrono...on::duration(3) from the C++ Standard Libary manual β yes, βLibaryβ with one r, we are not fixing the lore π
. The main character is std::chrono::duration::duration, and bestie it is literally the constructor page for std::chrono::duration::duration.
πΈ NAME πΈ
std::chrono::duration::duration β std::chrono::duration::duration. Thatβs the whole name, no cap.
β¨ Synopsis β¨
constexpr duration() = default; (1) (since C++11)
duration( const duration& ) = default; (2) (since C++11)
template< class Rep2 > (3) (since C++11)
constexpr explicit duration( const Rep2& r );
template< class Rep2, class Period2 > (4) (since C++11)
constexpr duration( const duration<Rep2, Period2>& d );
Constructs a new duration from one of several optional data sources.
- The default constructor.
- The copy constructor.
- Constructs a duration with
rticks.
This overload participates in overload resolution only if all following conditions are satisfied:
is_convertible<const Rep2&, Rep>::valueis true.- Any of the following conditions is satisfied:^[1]
std::chrono::treat_as_floating_point<Rep>::valueis true.std::chrono::treat_as_floating_point<Rep2>::valueis false.
- Constructs a duration by converting
dto an appropriate period and tick count, as if bystd::chrono::duration_cast<duration>(d).count().
This overload participates in overload resolution only if no overflow is induced in the conversion, and any of the following conditions is satisfied:^[2]
std::chrono::treat_as_floating_point<Rep>::valueis true.- All following conditions are satisfied:
std::ratio_divide<Period2, Period>::denis 1.std::chrono::treat_as_floating_point<Rep2>::valueis false.
^[1] β That is, a duration with an integer tick count cannot be constructed from a floating-point value, but a duration with a floating-point tick count can be constructed from an integer value.
^[2]β That is, either the duration uses floating-point ticks, orPeriod2is exactly divisible byPeriod.
π Parameters π
rβ a tick countdβ a duration to copy from
π¦ Example π¦
The following code shows several examples (both valid and invalid) of how to construct durations:
// Run this code
#include <chrono>
int main()
{
std::chrono::hours h(1); // one hour
std::chrono::milliseconds ms{3}; // 3 milliseconds
std::chrono::duration<int, std::kilo> ks(3); // 3000 seconds
// error: treat_as_floating_point<int>::value == false,
// This duration allows whole tick counts only
// std::chrono::duration<int, std::kilo> d3(3.5);
// 30Hz clock using fractional ticks
std::chrono::duration<double, std::ratio<1, 30>> hz30(3.5);
// 3000 microseconds constructed from 3 milliseconds
std::chrono::microseconds us = ms;
// error: 1/1000000 is not divisible by 1/1000
// std::chrono::milliseconds ms2 = us
std::chrono::duration<double, std::milli> ms2 = us; // 3.0 milliseconds
}
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 2094 β applied to C++11. Behavior as published: for overload (4),
std::ratio_divide<Period2, period>::nummight overflow when evaluatingstd::ratio_divide<Period2, period>::den. Correct behavior: overload (4) does not participate in overload resolution in this case. - LWG 3050 β applied to C++11. Behavior as published: convertibility constraint used non-const xvalue. Correct behavior: use const lvalues instead.
π See also π
operator=β assigns the contents (public member function)
byeeee bestie π
π β source/version and date vibes: http://cppreference.com, 2024.06.10, page std::chrono...on::duration(3).
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms