Namespaces
Variants
Views
Actions

std::chrono::duration::duration

From cppreference.com
constexpr duration();
(1)
template <class Rep2>
constexpr explicit duration(const Rep2& r);
(2)
template <class Rep2, class Period2>
constexpr duration(const duration<Rep2, Period2>& d);
(3)

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

1. Default constructor.

2. Constructs a duration with r ticks. Note that this constructor only participates in overload resolution if Rep2 is implicitly convertible to rep and

3. Constructs a duration by converting d to an appropriate period and tick count. In order to prevent truncation during conversion, this constructor only participates in overload resolution if:

or both:

[edit] Parameters

r - a tick count
d - a duration to copy from

[edit] Example

The following code shows several examples (both valid and invalid) of how to construct durations:

#include <iostream>
#include <chrono>
#include <thread>
 
int main()
{
    std::cout << "Waiting for the show to end..." << std::endl;
    std::chrono::duration<int, std::ratio<60>> num_minutes(22);
    std::this_thread::sleep_for(num_minutes);
    std::cout << "Show over!\n";
 
    // 3000 seconds
    std::chrono::duration<int, std::kilo> d2(3);
 
    // invalid because 3.5 is not implicitly convertible to int
    std::chrono::duration<int, std::kilo> d3(3.5);
 
    // 3 milliseconds
    std::chrono::duration<int, std::milli> ms(3);
 
    // 3000 microseconds
    std::chrono::duration<int, std::micro> us = ms;
 
    // invalid because of possible precision loss
    std::chrono::duration<int, std::milli> ms2 = us;
}

[edit] See also

assigns the contents
(public member function) [edit]