<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2020: Time utility arithmetic constexpr functions have invalid effects</title>
<meta property="og:title" content="Issue 2020: Time utility arithmetic constexpr functions have invalid effects">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2020.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="2020"><a href="lwg-defects.html#2020">2020</a>. Time utility arithmetic <code>constexpr</code> functions have invalid effects</h3>
<p><b>Section:</b> 30.5.6 <a href="https://wg21.link/time.duration.nonmember">[time.duration.nonmember]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2010-12-06 <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.nonmember">issues</a> in [time.duration.nonmember].</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>
As of issue <a href="lwg-defects.html#1171" title="duration types should be literal (Status: C++11)">1171</a><sup><a href="https://cplusplus.github.io/LWG/issue1171" title="Latest snapshot">(i)</a></sup> several time-utility functions have been marked <code>constexpr</code>.
Alas this was done without adapting the corresponding return elements, which has the effect that 
none of current arithmetic functions of class template <code>duration</code> marked as <code>constexpr</code> 
can ever be <code>constexpr</code> functions (which makes them ill-formed, no diagnostics required as 
of recent core rules), because they invoke a non-constant expression, e.g. 30.5.6 <a href="https://wg21.link/time.duration.nonmember">[time.duration.nonmember]</a>/2:
</p>
<blockquote><pre>
template &lt;class Rep1, class Period1, class Rep2, class Period2>
constexpr typename common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt;{&gt;}::type
operator+(const duration&lt;Rep1, Period1>&amp; lhs, const duration&lt;Rep2, Period2&gt;&amp; rhs);

2 Returns: CD(lhs) += rhs.
</pre></blockquote>
<p>
The real problem is, that we cannot defer to as-if rules here: The returns element
specifies an indirect calling contract of a potentially user-defined function. This <em>cannot</em> be
the <code>+=</code> assignment operator of such a user-defined type, but must be the corresponding
immutable binary <code>operator+</code> (unless we require that <code>+=</code> shall be an immutable function
which does not really makes sense).
</p>
<p><i>[2011-02-17 Reflector discussion]</i></p>

<p>
Moved to Tentatively Ready after 5 votes.
</p>



<p id="res-2020"><b>Proposed resolution:</b></p>
<p>
The suggested wording changes are against the working draft N3242. Additional to the normative wording
changes some editorial fixes are suggested.
</p>
<ol>
<li>
<p>In 30.5.6 <a href="https://wg21.link/time.duration.nonmember">[time.duration.nonmember]</a>, change the following arithmetic function specifications as follows:</p>
<blockquote><pre>
template &lt;class Rep1, class Period1, class Rep2, class Period2&gt;
constexpr typename common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt;<del>{</del>&gt;<del>}</del>::type
operator+(const duration&lt;Rep1, Period1&gt;&amp; lhs, const duration&lt;Rep2, Period2&gt;&amp; rhs);
</pre><blockquote><p>
2 <i>Returns</i>: <del><code>CD(lhs) += rhs</code></del><ins><code>CD(CD(lhs).count() + CD(rhs).count())</code></ins>.
</p></blockquote></blockquote>

<blockquote><pre>
template &lt;class Rep1, class Period1, class Rep2, class Period2&gt;
constexpr typename common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt;<del>{</del>&gt;<del>}</del>::type
operator-(const duration&lt;Rep1, Period1&gt;&amp; lhs, const duration&lt;Rep2, Period2&gt;&amp; rhs);
</pre><blockquote><p>
3 <i>Returns</i>: <del><code>CD(lhs) -= rhs</code></del><ins><code>CD(CD(lhs).count() - CD(rhs).count())</code></ins>.
</p></blockquote></blockquote>

<blockquote><pre>
template &lt;class Rep1, class Period, class Rep2&gt;
constexpr duration&lt;typename common_type&lt;Rep1, Rep2&gt;::type, Period&gt;
operator*(const duration&lt;Rep1, Period&gt;&amp; d, const Rep2&amp; s);
</pre><blockquote><p>
4 <i>Remarks</i>: This operator shall not participate in overload resolution unless <code>Rep2</code> is implicitly convertible
to <code>CR(Rep1, Rep2)</code>.
<p/>
5 <i>Returns</i>: <del><code>duration&lt;CR(Rep1, Rep2), Period&gt;(d) *= s</code></del><ins><code>CD(CD(d).count() * s)</code></ins>.
</p></blockquote></blockquote>

<blockquote><p>
<code>[...]</code>
</p></blockquote>

<blockquote><pre>
template &lt;class Rep1, class Period, class Rep2&gt;
constexpr duration&lt;typename common_type&lt;Rep1, Rep2&gt;::type, Period&gt;
operator/(const duration&lt;Rep1, Period&gt;&amp; d, const Rep2&amp; s);
</pre><blockquote><p>
8 <i>Remarks</i>: This operator shall not participate in overload resolution unless <code>Rep2</code> is implicitly convertible
to <code>CR(Rep1, Rep2)</code> and <code>Rep2</code> is not an instantiation of <code>duration</code>.
<p/>
9 <i>Returns</i>: <del><code>duration&lt;CR(Rep1, Rep2), Period&gt;(d) &#47;= s</code></del><ins><code>CD(CD(d).count() &#47; s)</code></ins>.
</p></blockquote></blockquote>

<blockquote><p>
<code>[...]</code>
</p></blockquote>

<blockquote><pre>
template &lt;class Rep1, class Period, class Rep2&gt;
constexpr duration&lt;typename common_type&lt;Rep1, Rep2&gt;::type, Period&gt;
operator%(const duration&lt;Rep1, Period&gt;&amp; d, const Rep2&amp; s);
</pre><blockquote><p>
11 <i>Remarks</i>: This operator shall not participate in overload resolution unless <code>Rep2</code> is implicitly convertible
to <code>CR(Rep1, Rep2)</code> and <code>Rep2</code> is not an instantiation of <code>duration</code>.
<p/>
12 <i>Returns</i>: <del><code>duration&lt;CR(Rep1, Rep2), Period&gt;(d) %= s</code></del><ins><code>CD(CD(d).count() % s)</code></ins>
</p></blockquote></blockquote>

<blockquote><pre>
template &lt;class Rep1, class Period1, class Rep2, class Period2&gt;
constexpr typename common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt;&gt;::type
operator%(const duration&lt;Rep1, Period1&gt;&amp; lhs, const duration&lt;Rep2, Period2&gt;&amp; rhs);
</pre><blockquote><p>
13 Returns: <del><code>common_type&lt;duration&lt;Rep1, Period1&gt;, duration&lt;Rep2, Period2&gt; &gt;::type(lhs) 
 %= rhs</code></del><ins><code>CD(CD(lhs).count() % CD(rhs).count())</code></ins>.
</p></blockquote></blockquote>
</li>
</ol>





</body>
</html>
