<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3090: What is &sect;[time.duration.cons]p4's "no overflow is induced in the conversion" intended to mean?</title>
<meta property="og:title" content="Issue 3090: What is &sect;[time.duration.cons]p4's &quot;no overflow is induced in the conversion&quot; intended to mean?">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3090.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#New">New</a> status.</em></p>
<h3 id="3090"><a href="lwg-active.html#3090">3090</a>. What is &sect;[time.duration.cons]p4's "no overflow is induced in the conversion" intended to mean?</h3>
<p><b>Section:</b> 30.5.2 <a href="https://wg21.link/time.duration.cons">[time.duration.cons]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Richard Smith <b>Opened:</b> 2018-03-22 <b>Last modified:</b> 2020-09-12</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#time.duration.cons">issues</a> in [time.duration.cons].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
30.5.2 <a href="https://wg21.link/time.duration.cons">[time.duration.cons]</a> p4 says:
</p>
<blockquote>
<pre>
template&lt;class Rep2, class Period2&gt;
  constexpr duration(const duration&lt;Rep2, Period2&gt;&amp; d);
</pre>
<blockquote>
<p>
<i>Remarks:</i> This constructor shall not participate in overload resolution unless no overflow is induced in the 
conversion and <code>treat_as_floating_point_v&lt;rep&gt;</code> is <code>true</code> or both 
<code>ratio_divide&lt;Period2, period&gt;::den</code> is 1 and <code>treat_as_floating_point_v&lt;Rep2&gt;</code> is <code>false</code>.
</p>
</blockquote>
</blockquote>
<p>
with this example:
</p>
<blockquote><pre>
duration&lt;int, milli&gt; ms(3);
duration&lt;int, micro&gt; us = ms;  <i>// OK</i>
duration&lt;int, milli&gt; ms2 = us; <i>// error</i>
</pre></blockquote>
<p>
It's unclear to me what "no overflow is induced in the conversion" means in the above. What happens here:
</p>
<blockquote><pre>
duration&lt;int, milli&gt; ms(INT_MAX);
duration&lt;int, micro&gt; us = ms;  <i>// ???</i>
</pre></blockquote>
<p>
An overflow is clearly induced in the conversion here: internally, we'll multiply <code>INT_MAX</code> by 1000. But that 
cannot be determined statically (in general), and so can't affect the result of overload resolution.
<p/>
So what's actually supposed to happen? Are we actually just supposed to check that <code>Rep2</code> is no larger than <code>Rep</code>? 
(If so, what happens on overflow? Undefined behavior?)
<p/>
It has been pointed out by Howard Hinnant:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
This refers to the compile-time conversion factor to convert <code>Period2</code> to <code>Period</code>. If that conversion factor 
is not representable as a (reduced) <code>ratio&lt;N, D&gt;</code>, then the constructor is SFINAE'd out. This might happen 
(for example) converting years to picoseconds.
</blockquote>
<p>
I would not have guessed that from the wording. Maybe replacing "no overflow is induced in the conversion" with "the result 
of <code>ratio_divide&lt;Period2, Period&gt;</code> is representable as a <code>ratio</code>" or similar would help? 
</p>

<p><i>[2018-06-18 after reflector discussion]</i></p>

<p>Priority set to 3</p>

<p><i>[2020-09-12 Jonathan adds a proposed resolution]</i></p>

<p>
Since the result of the <code>ratio_divide</code> has to be a <code>ratio</code>,
if it's not representable then the result simply isn't a valid type.
Implementations are not required to make <code>ratio_divide</code> SFINAE-friendly
to implement this constraint. They can perform the equivalent calculations
to check if they would overflow, without actually using <code>ratio_divide</code>.
</p>



<p id="res-3090"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="http://wg21.link/n4861">N4861</a>.
</p>

<ol>
<li><p>Modify 30.5.2 <a href="https://wg21.link/time.duration.cons">[time.duration.cons]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class Rep2, class Period2&gt;
  constexpr duration(const duration&lt;Rep2, Period2&gt;&amp; d);
</pre>
</blockquote>
<p>
-3- <i>Constraints:</i>
<ins>
<code>is_convertible_v&lt;const Rep2&amp;, rep&gt;</code> is <code>true</code>.
<code>ratio_divide&lt;typename Period2::type, period&gt;</code> is
a valid <code>ratio</code> specialization.
Either:
</ins>
</p>
<ul>
<li>
<ins>
<code>treat_as_floating_point_v&lt;rep&gt;</code> is <code>true</code>; or
</ins>
</li>
<li>
<ins>
<code>ratio_divide&lt;Period2, period&gt;::den</code> is <code>1</code>
and <code>treat_as_floating_point_v&lt;Rep2&gt;</code> is <code>false</code>.
</ins>
</li>
</ul>
<p>
<del>
No overflow is induced in the conversion and
<code>treat_as_floating_point_v&lt;rep&gt;</code> is <code>true</code>
or both <code>ratio_divide&lt;Period2, period&gt;::den</code> is <code>1</code>
and <code>treat_as_floating_point_v&lt;Rep2&gt;</code> is <code>false</code>.
</del>
[<i>Note:</i> This requirement prevents implicit truncation error<ins>s</ins>
when converting between integral-based <code>duration</code> types.
Such a construction could easily lead to confusion about the value of the
<code>duration</code>.
&mdash; <i>end note</i>]
</p>
</li>
</ol>





</body>
</html>
