<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2094: duration conversion overflow shouldn't participate in overload resolution</title>
<meta property="og:title" content="Issue 2094: duration conversion overflow shouldn't participate in overload resolution">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2094.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++14">C++14</a> status.</em></p>
<h3 id="2094"><a href="lwg-defects.html#2094">2094</a>. <code>duration</code> conversion overflow shouldn't participate in overload resolution</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#C++14">C++14</a>
 <b>Submitter:</b> Vicente J. Botet Escriba <b>Opened:</b> 2011-10-31 <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#time.duration.cons">issues</a> in [time.duration.cons].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
30.5.2 <a href="https://wg21.link/time.duration.cons">[time.duration.cons]</a> 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 
<code>treat_as_floating_point&lt;rep&gt;::value</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&lt;Rep2&gt;::value</code> 
is <code>false</code>.
</p></blockquote></blockquote>
<p>
The evaluation of <code>ratio_divide&lt;Period2, period&gt;::den</code> could make 
<code>ratio_divide&lt;Period2, period&gt;::num</code> overflow.
<p/>
This occur for example when we try to create a millisecond (<code>period</code>=<code>ratio&lt;1,1000&gt;</code>) 
from an exa-second (<code>Period2</code>=<code>ratio&lt;10<sup>18</sup>&gt;</code>).
<p/>
<code>ratio_divide&lt;ratio&lt;10<sup>18</sup>&gt;, ratio&lt;1,1000&gt;&gt;::num</code> is 
<code>10<sup>21</sup></code> which overflows which makes the compiler error.
<p/>
If the function <code>f</code> is overloaded with milliseconds and seconds
</p>
<blockquote><pre>
void f(milliseconds);
void f(seconds);
</pre></blockquote>
<p>
The following fails to compile.
</p>
<blockquote><pre>
duration&lt;int,exa&gt; r(1);
f(r);
</pre></blockquote>
<p>
While the conversion to seconds work, the conversion to milliseconds make the program fail at compile time. 
In my opinion, this program should be well formed and the constructor from <code>duration&lt;int,exa&gt;</code> 
to milliseconds shouldn't participate in overload resolution as the result can not be represented.
<p/>
I think the wording of the standard can be improved so no misinterpretations are possible by adding that 
"no overflow is induced by the conversion".
</p>

<p><i>[2012, Kona]</i></p>

<p>
Move to Review.
</p>
<p>Pete: The wording is not right.</p>
<p>Howard: Will implement this to be sure it works.</p>
<p>Jeffrey: If ratio needs a new hook, should it be exposed to users for their own uses?</p>
<p>Pete: No.</p>

<p>
Move to Review,
Howard to implement in a way that mere mortals can understand.
</p>

<p><i>[2013-04-18, Bristol]</i></p>




<p id="res-2094"><b>Proposed resolution:</b></p>
<p>This wording is relative to the FDIS.</p>

<p>Change the following paragraphs of 30.5.2 <a href="https://wg21.link/time.duration.cons">[time.duration.cons]</a> p4 indicated:</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 <ins>no 
overflow is induced in the conversion and</ins> <code>treat_as_floating_point&lt;rep&gt;::value</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&lt;Rep2&gt;::value</code> is <code>false</code>. [ <i>Note</i>: This 
requirement prevents implicit truncation error when converting between integral-based duration 
types. Such a construction could easily lead to confusion about the value of the 
duration. &mdash; <i>end note</i> ]
</p></blockquote></blockquote>






</body>
</html>
