<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 921: Rational Arithmetic should use template aliases</title>
<meta property="og:title" content="Issue 921: Rational Arithmetic should use template aliases">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue921.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++11">C++11</a> status.</em></p>
<h3 id="921"><a href="lwg-defects.html#921">921</a>. Rational Arithmetic should use template aliases</h3>
<p><b>Section:</b> 21.5.3 <a href="https://wg21.link/ratio.ratio">[ratio.ratio]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Pablo Halpern <b>Opened:</b> 2008-10-07 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#ratio.ratio">issues</a> in [ratio.ratio].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The compile-time functions that operate on <code>ratio&lt;N,D&gt;</code> require the
cumbersome and error-prone "evaluation" of a <code>type</code> member using a
meta-programming style that predates the invention of template aliases.
Thus, multiplying three ratios <code>a</code>, <code>b</code>, and <code>c</code> requires the expression:
</p>

<blockquote><pre>
ratio_multiply&lt;a, ratio_multiply&lt;b, c&gt;::type&gt;::type
</pre></blockquote>

<p>
The simpler expression:
</p>

<blockquote><pre>
ratio_multiply&lt;a, ratio_multiply&lt;b, c&gt;&gt;
</pre></blockquote>

<p>
Could be used by if template aliases were employed in the definitions.
</p>

<p><i>[
Post Summit:
]</i></p>


<blockquote>
<p>
Jens: not a complete proposed resolution: "would need to make similar change"
</p>
<p>
Consensus: We agree with the direction of the issue.
</p>
<p>
Recommend Open.
</p>
</blockquote>

<p><i>[
2009-05-11 Daniel adds:
]</i></p>


<blockquote>
<p>
Personally I'm <em>not</em> in favor for the addition of:
</p>
<blockquote><pre>
typedef ratio type;
</pre></blockquote>
<p>
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.
</p>
</blockquote>

<p><i>[
2009-05-11 Pablo adds:
]</i></p>


<blockquote>
<p>
The addition of type to the <code>ratio</code> 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.
</p>
</blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote><p>
Bill asks for additional discussion in the issue
that spells out more details of the implementation.
Howard points us to issue <a href="lwg-defects.html#948" title="ratio arithmetic tweak (Status: C++11)">948</a><sup><a href="https://cplusplus.github.io/LWG/issue948" title="Latest snapshot">(i)</a></sup>
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.
</p></blockquote>

<p><i>[
2009-07-21 Alisdair adds:
]</i></p>


<blockquote><p>
See <a href="lwg-closed.html#1121" title="Support for multiple arguments (Status: NAD)">1121</a><sup><a href="https://cplusplus.github.io/LWG/issue1121" title="Latest snapshot">(i)</a></sup> for a potentially incompatible proposal.
</p></blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Move to Ready.
</p></blockquote>



<p id="res-921"><b>Proposed resolution:</b></p>

 
 <ol>
<li>
<p>
In 21.5 <a href="https://wg21.link/ratio">[ratio]</a> p.3 change as indicated:
</p>

<blockquote><pre>
// ratio arithmetic
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_add<ins> = <em>see below</em></ins>;
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_subtract<ins> = <em>see below</em></ins>;
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_multiply<ins> = <em>see below</em></ins>;
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_divide<ins> = <em>see below</em></ins>;
</pre></blockquote>
</li>
<li>
<p>
In 21.5.3 <a href="https://wg21.link/ratio.ratio">[ratio.ratio]</a>, change as indicated:
</p>
<blockquote><pre>
namespace std {
  template &lt;intmax_t N, intmax_t D = 1&gt;
  class ratio {
  public:
    <ins>typedef ratio type;</ins>
    static const intmax_t num;
    static const intmax_t den;
  };
}
</pre></blockquote>
</li>
<li>
<p>
In 21.5.4 <a href="https://wg21.link/ratio.arithmetic">[ratio.arithmetic]</a> change as indicated:
</p>

<blockquote><pre>
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_add<ins> = <em>see below</em></ins><del>{
  typedef <em>see below</em> type;
}</del>;
</pre>

<blockquote>
<p>
1 The <del>nested typedef</del> type <code><ins>ratio_add&lt;R1, R2&gt;</ins></code>
shall be a synonym for <code>ratio&lt;T1, T2&gt;</code>
where <code>T1</code> has the value <code>R1::num * R2::den + R2::num * R1::den</code> and <code>T2</code>
has the value <code>R1::den * R2::den</code>.
</p>
</blockquote>
</blockquote>
<blockquote><pre>
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_subtract<ins> = <em>see below</em></ins><del>{
  typedef <em>see below</em> type;
}</del>;
</pre>
<blockquote>
<p>
2 The <del>nested typedef</del> type <code><ins>ratio_subtract&lt;R1, R2&gt;</ins></code>
shall be a synonym for <code>ratio&lt;T1, T2&gt;</code>
where <code>T1</code> has the value <code>R1::num * R2::den - R2::num * R1::den</code> and <code>T2</code>
has the value <code>R1::den * R2::den</code>.
</p>
</blockquote>
</blockquote>
<blockquote><pre>
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_multiply<ins> = <em>see below</em></ins><del>{
  typedef <em>see below</em> type;
}</del>;
</pre>
<blockquote>
<p>
3 The <del>nested typedef</del> type <code><ins>ratio_multiply&lt;R1, R2&gt;</ins></code>
shall be a synonym for <code>ratio&lt;T1, T2&gt;</code>
where <code>T1</code> has the value <code>R1::num * R2::num</code> and <code>T2</code> has the value <code>R1::den * R2::den</code>.
</p>
</blockquote>
</blockquote>
<blockquote><pre>
template &lt;class R1, class R2&gt; <del>struct</del><ins>using</ins> ratio_divide<ins> = <em>see below</em></ins><del>{
  typedef <em>see below</em> type;
}</del>;
</pre>
<blockquote>
<p>
4 The <del>nested typedef</del> type <code><ins>ratio_divide&lt;R1, R2&gt;</ins></code>
shall be a synonym for <code>ratio&lt;T1, T2&gt;</code>
where <code>T1</code> has the value <code>R1::num * R2::den</code> and <code>T2</code> has the value <code>R1::den * R2::num</code>.
</p>
</blockquote>
</blockquote>
</li>
<li>
<p>
In 30.5.2 <a href="https://wg21.link/time.duration.cons">[time.duration.cons]</a> p.4 change as indicated:
</p>
<blockquote>
<p>
<i>Requires:</i> <code>treat_as_floating_point&lt;rep&gt;::value</code> shall be true or
<code>ratio_divide&lt;Period2, period&gt;::<del>type::</del>den</code> shall be 1.[..]
</p>
</blockquote>
</li>
<li>
<p>
In 30.5.8 <a href="https://wg21.link/time.duration.cast">[time.duration.cast]</a> p.2 change as indicated:
</p>
<blockquote>
<p>
<i>Returns:</i> Let CF be <code>ratio_divide&lt;Period, typename
ToDuration::period&gt;<del>::type</del></code>, and [..]
</p>
</blockquote>
</li>
</ol>





</body>
</html>
