constexpr duration() = default; | (1) | (since C++11) |
duration( const duration& ) = default; | (2) | (since C++11) |
template< class Rep2 > constexpr explicit duration( const Rep2& r ); | (3) | (since C++11) |
template< class Rep2, class Period2 > constexpr duration( const duration<Rep2,Period2>& d ); | (4) | (since C++11) |
Constructs a new duration
from one of several optional data sources.
const Rep2&
std::chrono::treat_as_floating_point<rep>::value
is true, or std::chrono::treat_as_floating_point<Rep2>::value
is false. d
to an appropriate period and tick count, as if by std::chrono::duration_cast<duration>(d).count()
. In order to prevent truncation during conversion, this constructor only participates in overload resolution if computation of the conversion factor (by std::ratio_divide<Period2, Period>
) does not overflow and:std::chrono::treat_as_floating_point<rep>::value == true
std::ratio_divide<Period2, period>::den == 1
, and std::chrono::treat_as_floating_point<Rep2>::value == false
. Period2
is exactly divisible by period)r | - | a tick count |
d | - | a duration to copy from |
The following code shows several examples (both valid and invalid) of how to construct durations:
#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 }
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3050 | C++11 | convertibility constraint used non-const xvalue | use const lvalues instead |
assigns the contents (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/chrono/duration/duration