liveBook · Manning

Ratio_multiply

As explained in Learn C++ by Example

ratio_multiply is a C++ type trait that computes the compile-time product of two rational ratio types.

ratio_multiply computes the compile-time product of two rational ratio types. In chrono code, it combines years::period with std::hecto to define a century duration without manually calculating the fraction. section 4.2.2

Defining centuries

using centuries = std::chrono::duration<long long, std::ratio_multiply<std::chrono::years::period, std::hecto>>; centuries two_hundred_years{2}; seconds sec = two_hundred_years; hours hrs = two_hundred_years; days days_count = duration_cast<days>(two_hundred_years);

How Learn C++ by Example covers it

How deep it goes
Explains ratio composition through a century alias and stops short of detailing ratio arithmetic internals.
Examples
Demonstrates a centuries type and converts two centuries to seconds, hours, and approximately 73,048 days.
What it assumes
Assumes familiarity with chrono durations and type aliases while showing how to compose an unfamiliar period.
What you can do after
Apply it via a C++ type alias to define a custom chrono duration with a calculated period.
How it connects
Connects std::ratio, std::chrono::duration, std::chrono::years, std::hecto, and duration_cast.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.