This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++11 status.

921. Rational Arithmetic should use template aliases

Section: 21.4.3 [ratio.ratio] Status: C++11 Submitter: Pablo Halpern Opened: 2008-10-07 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [ratio.ratio].

View all issues with C++11 status.

Discussion:

The compile-time functions that operate on ratio<N,D> require the cumbersome and error-prone "evaluation" of a type member using a meta-programming style that predates the invention of template aliases. Thus, multiplying three ratios a, b, and c requires the expression:

ratio_multiply<a, ratio_multiply<b, c>::type>::type

The simpler expression:

ratio_multiply<a, ratio_multiply<b, c>>

Could be used by if template aliases were employed in the definitions.

[ Post Summit: ]

Jens: not a complete proposed resolution: "would need to make similar change"

Consensus: We agree with the direction of the issue.

Recommend Open.

[ 2009-05-11 Daniel adds: ]

Personally I'm not in favor for the addition of:

typedef ratio type;

For a reader of the standard it's usage or purpose is unclear. I haven't seen similar examples of attempts to satisfy non-feature complete compilers.

[ 2009-05-11 Pablo adds: ]

The addition of type to the ratio template allows the previous style (i.e., in the prototype implementations) to remain valid and permits the use of transitional library implementations for C++03 compilers. I do not feel strongly about its inclusion, however, and leave it up to the reviewers to decide.

[ Batavia (2009-05): ]

Bill asks for additional discussion in the issue that spells out more details of the implementation. Howard points us to issue 948 which has at least most of the requested details. Tom is strongly in favor of overflow-checking at compile time. Pete points out that there is no change of functionality implied. We agree with the proposed resolution, but recommend moving the issue to Review to allow time to improve the discussion if needed.

[ 2009-07-21 Alisdair adds: ]

See 1121 for a potentially incompatible proposal.

[ 2009-10 Santa Cruz: ]

Move to Ready.

Proposed resolution:

  1. In 21.4 [ratio] p.3 change as indicated:

    // ratio arithmetic
    template <class R1, class R2> structusing ratio_add = see below;
    template <class R1, class R2> structusing ratio_subtract = see below;
    template <class R1, class R2> structusing ratio_multiply = see below;
    template <class R1, class R2> structusing ratio_divide = see below;
    
  2. In 21.4.3 [ratio.ratio], change as indicated:

    namespace std {
      template <intmax_t N, intmax_t D = 1>
      class ratio {
      public:
        typedef ratio type;
        static const intmax_t num;
        static const intmax_t den;
      };
    }
    
  3. In 21.4.4 [ratio.arithmetic] change as indicated:

    template <class R1, class R2> structusing ratio_add = see below{
      typedef see below type;
    };
    

    1 The nested typedef type ratio_add<R1, R2> shall be a synonym for ratio<T1, T2> where T1 has the value R1::num * R2::den + R2::num * R1::den and T2 has the value R1::den * R2::den.

    template <class R1, class R2> structusing ratio_subtract = see below{
      typedef see below type;
    };
    

    2 The nested typedef type ratio_subtract<R1, R2> shall be a synonym for ratio<T1, T2> where T1 has the value R1::num * R2::den - R2::num * R1::den and T2 has the value R1::den * R2::den.

    template <class R1, class R2> structusing ratio_multiply = see below{
      typedef see below type;
    };
    

    3 The nested typedef type ratio_multiply<R1, R2> shall be a synonym for ratio<T1, T2> where T1 has the value R1::num * R2::num and T2 has the value R1::den * R2::den.

    template <class R1, class R2> structusing ratio_divide = see below{
      typedef see below type;
    };
    

    4 The nested typedef type ratio_divide<R1, R2> shall be a synonym for ratio<T1, T2> where T1 has the value R1::num * R2::den and T2 has the value R1::den * R2::num.

  4. In 29.5.2 [time.duration.cons] p.4 change as indicated:

    Requires: treat_as_floating_point<rep>::value shall be true or ratio_divide<Period2, period>::type::den shall be 1.[..]

  5. In 29.5.8 [time.duration.cast] p.2 change as indicated:

    Returns: Let CF be ratio_divide<Period, typename ToDuration::period>::type, and [..]