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
- std::chrono::treat_as_floating_point<rep>::value is true, or
- std::chrono::treat_as_floating_point<Rep2>::value is false.
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:
- std::chrono::treat_as_floating_point<rep>::value == true
or both:
- std::ratio_divide<Period2, period>::den == 1, and
- std::chrono::treat_as_floating_point<Rep2>::value == false.
[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) |