Documentation Home

Construct a std::chrono::duration instance with a count determined by scaling another std::chrono::duration value.

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

Effects:

The internal value of the duration object is initialized with duration_cast<duration<Rep,Period>>(d).count().

Requirements:

This constructor only participates in overload resolution if Rep is a floating point type or Rep2 is not a floating point type and Period2 is a whole number multiple of Period (that is, ratio_divide<Period2,Period>::den==1). This avoids accidental truncation (and corresponding loss of precision) from storing a duration with small periods in a variable representing a duration with a longer period.

Postcondition:

this->count()==duration_cast<duration<Rep,Period>>(d).count()

Examples
duration<int,ratio<1,1000>> ms(5); // 5 milliseconds
duration<int,ratio<1,1>> s(ms); // Error: can't store milliseconds as a whole number of seconds
duration<double,ratio<1,1>> s2(ms); // OK:  can store milliseconds as fractional seconds, s2.count()==0.005
duration<int,ratio<1,1000000>> us(ms); // OK: can't store milliseconds has a whole number of microseconds, us.count()==5000
Header

#include <chrono>

See Also