<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 947: duration arithmetic: contradictory requirements</title>
<meta property="og:title" content="Issue 947: duration arithmetic: contradictory requirements">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue947.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#Resolved">Resolved</a> status.</em></p>
<h3 id="947"><a href="lwg-defects.html#947">947</a>. <code>duration</code> arithmetic: contradictory 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#Resolved">Resolved</a>
 <b>Submitter:</b> Pete Becker <b>Opened:</b> 2008-12-20 <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#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In 30.5.6 <a href="https://wg21.link/time.duration.nonmember">[time.duration.nonmember]</a>, paragraph 8 says that calling
<code>dur / rep</code> when <code>rep</code> is an instantiation of <code>duration</code> 
requires a diagnostic. That's followed by an <code>operator/</code> that takes 
two durations. So <code>dur1 / dur2</code> is legal under the second version,
but requires a diagnostic under the first.
</p>

<p><i>[
Howard adds:
]</i></p>


<blockquote><p>
Please see the thread starting with c++std-lib-22980 for more information.
</p></blockquote>

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

<blockquote><p>
Move to Open, pending proposed wording (and preferably an implementation).
</p></blockquote>

<p><i>[
2009-07-27 Howard adds:
]</i></p>


<blockquote>
<p>
I've addressed this issue under the proposed wording for <a href="lwg-defects.html#1177" title="Improve &quot;diagnostic required&quot; wording (Status: C++11)">1177</a><sup><a href="https://cplusplus.github.io/LWG/issue1177" title="Latest snapshot">(i)</a></sup> which
cleans up several places under 30.5 <a href="https://wg21.link/time.duration">[time.duration]</a> which used the
phrase "diagnostic required".
</p>
<p>
For clarity's sake, here is an example implementation of the constrained <code>operator/</code>:
</p>

<blockquote><pre>
template &lt;class _Duration, class _Rep, bool = __is_duration&lt;_Rep&gt;::value&gt;
struct __duration_divide_result
{
};

template &lt;class _Duration, class _Rep2,
    bool = is_convertible&lt;_Rep2,
                          typename common_type&lt;typename _Duration::rep, _Rep2&gt;::type&gt;::value&gt;
struct __duration_divide_imp
{
};

template &lt;class _Rep1, class _Period, class _Rep2&gt;
struct __duration_divide_imp&lt;duration&lt;_Rep1, _Period&gt;, _Rep2, true&gt;
{
    typedef duration&lt;typename common_type&lt;_Rep1, _Rep2&gt;::type, _Period&gt; type;
};

template &lt;class _Rep1, class _Period, class _Rep2&gt;
struct __duration_divide_result&lt;duration&lt;_Rep1, _Period&gt;, _Rep2, false&gt;
    : __duration_divide_imp&lt;duration&lt;_Rep1, _Period&gt;, _Rep2&gt;
{
};

template &lt;class _Rep1, class _Period, class _Rep2&gt;
inline
typename __duration_divide_result&lt;duration&lt;_Rep1, _Period&gt;, _Rep2&gt;::type
operator/(const duration&lt;_Rep1, _Period&gt;&amp; __d, const _Rep2&amp; __s)
{
    typedef typename common_type&lt;_Rep1, _Rep2&gt;::type _Cr;
    duration&lt;_Cr, _Period&gt; __r = __d;
    __r /= static_cast&lt;_Cr&gt;(__s);
    return __r;
}
</pre></blockquote>

<p>
<code>__duration_divide_result</code> is basically a custom-built <code>enable_if</code>
that will contain <code>type</code> only if <code>Rep2</code> is not a <code>duration</code>
and if <code>Rep2</code> is implicitly convertible to
<code>common_type&lt;typename Duration::rep, Rep2&gt;::type</code>. <code>__is_duration</code>
is simply a private trait that answers <code>false</code>, but is specialized for
<code>duration</code> to answer <code>true</code>.
</p>

<p>
The constrained <code>operator%</code> works identically.
</p>
</blockquote>

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


<blockquote><p>
Mark <del>NAD Editorial</del><ins>Resolved</ins>, fixed by <a href="lwg-defects.html#1177" title="Improve &quot;diagnostic required&quot; wording (Status: C++11)">1177</a><sup><a href="https://cplusplus.github.io/LWG/issue1177" title="Latest snapshot">(i)</a></sup>.
</p></blockquote>



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





</body>
</html>
