|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
constexpr duration()=default; | (1) | (since C++11) |
duration(const duration&)=default; | (2) | (since C++11) |
template<class Rep2> constexprexplicit 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 newduration from one of several optional data sources.
Period2 is exactly divisible byPeriod.Contents |
| 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 hourstd::chrono::milliseconds ms{3};// 3 millisecondsstd::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 ticksstd::chrono::duration<double,std::ratio<1,30>> hz30(3.5); // 3000 microseconds constructed from 3 millisecondsstd::chrono::microseconds us= ms;// error: 1/1000000 is not divisible by 1/1000// std::chrono::milliseconds ms2 = usstd::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 2094 | C++11 | for overload(4),std::ratio_divide<Period2, period>::num might overflow when evaluating std::ratio_divide<Period2, period>::den | overload(4) does not participate in overload resolution in this case |
| LWG 3050 | C++11 | convertibility constraint used non-const xvalue | use const lvalues instead |
| assigns the contents (public member function)[edit] |