<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 882: duration non-member arithmetic requirements</title>
<meta property="og:title" content="Issue 882: duration non-member arithmetic requirements">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue882.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#CD1">CD1</a> status.</em></p>
<h3 id="882"><a href="lwg-defects.html#882">882</a>. <code>duration</code> non-member arithmetic requirements</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#CD1">CD1</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2008-09-08 <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#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm">N2661</a>
specified the following requirements for the non-member <code>duration</code>
arithmetic:
</p>

<blockquote>

<pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  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>
<i>Requires:</i> Let <code>CR</code> represent the <code>common_type</code> of <code>Rep1</code> and
<code>Rep2</code>.  Both <code>Rep1</code> and <code>Rep2</code> shall be implicitly convertible
to CR, diagnostic required.
</p></blockquote>

<pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  duration&lt;typename common_type&lt;Rep1, Rep2&gt;::type, Period&gt;
  operator*(const Rep1&amp; s, const duration&lt;Rep2, Period&gt;&amp; d);
</pre>

<blockquote><p>
<i>Requires:</i> Let <code>CR</code> represent the <code>common_type</code> of
<code>Rep1</code> and <code>Rep2</code>. Both <code>Rep1</code> and <code>Rep2</code>
shall be implicitly convertible to <code>CR</code>, diagnostic required.
</p></blockquote>

<pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  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>
<i>Requires:</i> Let <code>CR</code> represent the <code>common_type</code> of
<code>Rep1</code> and <code>Rep2</code>. Both <code>Rep1</code> and <code>Rep2</code> shall be
implicitly convertible to <code>CR</code>, and <code>Rep2</code> shall not be an
instantiation of <code>duration</code>, diagnostic required.
</p></blockquote>

</blockquote>

<p>
During transcription into the working paper, the requirements clauses on these
three functions was changed to:
</p>

<blockquote><p>
<i>Requires:</i> <code>CR(Rep1, Rep2)</code> shall exist. Diagnostic required.
</p></blockquote>

<p>
This is a non editorial change with respect to
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm">N2661</a>
as user written representations which are used in <code>duration</code> need not be
implicitly convertible to or from arithmetic types in order to interoperate with
<code>duration</code>s based on arithmetic types.  An explicit conversion will do
fine for most expressions as long as there exists a <code>common_type</code> specialization
relating the user written representation and the arithmetic type.  For example:
</p>

<blockquote><pre>
class saturate
{
public:
  explicit saturate(long long i);
  ...
};

namespace std {

template &lt;&gt;
struct common_type&lt;saturate, long long&gt;
{
    typedef saturate type;
};

template &lt;&gt;
struct common_type&lt;long long, saturate&gt;
{
    typedef saturate type;
};

}  // std

millisecond ms(3);  // integral-based duration
duration&lt;saturate, milli&gt; my_ms = ms;  // ok, even with explicit conversions
my_ms = my_ms + ms;                    // ok, even with explicit conversions
</pre></blockquote>

<p>
However, when dealing with multiplication of a duration and its representation,
implicit convertibility is required between the rhs and the lhs's representation
for the member <code>*=</code> operator:
</p>

<blockquote><pre>
template &lt;class Rep, class Period = ratio&lt;1&gt;&gt; 
class duration { 
public: 
   ...
   duration&amp; operator*=(const rep&amp; rhs);
   ...
};
...
ms *= 2;               // ok, 2 is implicitly convertible to long long
my_ms *= saturate(2);  // ok, rhs is lhs's representation
my_ms *= 2;            // error, 2 is not implicitly convertible to saturate
</pre></blockquote>

<p>
The last line does not (and should not) compile.  And we want non-member multiplication
to have the same behavior as member arithmetic:
</p>

<blockquote><pre>
my_ms = my_ms * saturate(2);  // ok, rhs is lhs's representation
my_ms = my_ms * 2;            // <em>should be</em> error, 2 is not implicitly convertible to saturate
</pre></blockquote>

<p>
The requirements clauses of
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm">N2661</a>
make the last line an error as expected.  However the latest working draft at
this time
(<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf">N2723</a>)
allows the last line to compile.
</p>

<p>
All that being said, there does appear to be an error in these requirements clauses
as specified by 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm">N2661</a>.
</p>

<blockquote><p>
<i>Requires:</i> ... <em>Both</em> <code>Rep1</code> and <code>Rep2</code> shall be implicitly convertible
to CR, diagnostic required.
</p></blockquote>

<p>
It is not necessary for both <code>Rep</code>s to be <i>implicitly</i> convertible to
the <code>CR</code>.  It is only necessary for the rhs <code>Rep</code> to be implicitly
convertible to the <code>CR</code>.  The <code>Rep</code> within the <code>duration</code>
should be allowed to only be explicitly convertible to the <code>CR</code>.  The
explicit-conversion-requirement is covered under 30.5.8 <a href="https://wg21.link/time.duration.cast">[time.duration.cast]</a>.
</p>



<p id="res-882"><b>Proposed resolution:</b></p>
<p>
Change the requirements clauses under 30.5.6 <a href="https://wg21.link/time.duration.nonmember">[time.duration.nonmember]</a>:
</p>

<blockquote>

<pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  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>
<i>Requires:</i> <del><code>CR(Rep1, Rep2)</code> shall exist.</del>
<ins><code>Rep2</code> shall be implicitly convertible to <code>CR(Rep1, Rep2)</code>.</ins>
Diagnostic required.
</p></blockquote>

<pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  duration&lt;typename common_type&lt;Rep1, Rep2&gt;::type, Period&gt;
  operator*(const Rep1&amp; s, const duration&lt;Rep2, Period&gt;&amp; d);
</pre>

<blockquote><p>
<i>Require<ins>s</ins><del>d behavior</del>:</i> <del><code>CR(Rep1, Rep2)</code> shall exist.</del>
<ins><code>Rep1</code> shall be implicitly convertible to <code>CR(Rep1, Rep2)</code>.</ins>
Diagnostic required.
</p></blockquote>

<pre>
template &lt;class Rep1, class Period, class Rep2&gt;
  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>
<i>Requires:</i> <del><code>CR(Rep1, Rep2)</code> shall exist</del>
<ins><code>Rep2</code> shall be implicitly convertible to <code>CR(Rep1, Rep2)</code></ins>
and <code>Rep2</code> shall not
be an instantiation of <code>duration</code>. Diagnostic required.
</p></blockquote>

</blockquote>





</body>
</html>
