Documentation Home >> Headers >> <ratio> Header >> std::ratio_add class template

The std::ratio_add class template provides a mechanism for adding two std::ratio values at compile time, using rational arithmetic.

template <class R1, class R2>
class ratio_add
    typedef std::ratio<see below> type;

R1 and R2 must be instantiations of the std::ratio class template.

The member typedef type is defined to be an instantiation of std::ratio that represents the sum of the fractions represented by R1 and R2 if that sum can be calculated without overflow. In the absence of arithmetic overflow, std::ratio_add<R1,R2>::type shall have the same num and den values as std::ratio<R1::num * R2::den + R2::num * R1::den, R1::den * R2::den>.

    std::ratio<1,3>, std::ratio<2,5> >::type::num == 11
    std::ratio<1,3>, std::ratio<2,5> >::type::den == 15

    std::ratio<1,3>, std::ratio<7,6> >::type::num == 3
    std::ratio<1,3>, std::ratio<7,6> >::type::den == 2

#include <ratio>

See Also