< cpp‎ | chrono‎ | duration
Revision as of 16:34, 2 November 2012 by P12bot (Talk | contribs)


Template:ddcl list begin <tr class="t-dcl ">

<td >
constexpr duration() = default;

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
duration(const duration&) = default;

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template <class Rep2>
constexpr explicit duration(const Rep2& r);

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template <class Rep2, class Period2>
constexpr duration(const duration<Rep2, Period2>& d);

<td > (4) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Constructs a new duration from one of several optional data sources.

1) The default constructor is defaulted.
2) The copy constructor is defaulted (makes a bitwise copy of the tick count).
3) Constructs a duration with r ticks. Note that this constructor only participates in overload resolution if Rep2 (the argument type) is implicitly convertible to rep (the type of this duration's ticks) and
(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)
4) Constructs a duration by converting 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:
or both:
(that is, either the duration uses floating-point ticks, or 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

See also

Template:cpp/chrono/duration/dcl list operator=