<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2141: common_type trait produces reference types</title>
<meta property="og:title" content="Issue 2141: common_type trait produces reference types">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2141.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="2141"><a href="lwg-defects.html#2141">2141</a>. <code>common_type</code> trait produces reference types</h3>
<p><b>Section:</b> 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Doug Gregor <b>Opened:</b> 2012-03-11 <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#meta.trans.other">issues</a> in [meta.trans.other].</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>
The type computation of the <code>common_type</code> type trait is defined as
</p>
<blockquote><pre>
template &lt;class T, class U&gt;
 struct common_type&lt;T, U&gt; {
   typedef decltype(true ? declval&lt;T&gt;() : declval&lt;U&gt;()) type;
 };
</pre></blockquote>
<p>
This means that <code>common_type&lt;int, int&gt;::type</code> is <code>int&amp;&amp;</code>, because
</p>
<ul>
<li><code>declval&lt;int&gt;()</code> returns <code>int&amp;&amp;</code></li>
<li>The conditional operator returns an xvalue when its second and third operands have the same type 
and are both xvalues (7.6.16 <a href="https://wg21.link/expr.cond">[expr.cond]</a> p4)</li>
<li><code>decltype</code> returns <code>T&amp;&amp;</code> when its expression is an xvalue (9.2.9.3 <a href="https://wg21.link/dcl.type.simple">[dcl.type.simple]</a> p4)</li>
</ul>
<p>
Users of <code>common_type</code> do not expect to get a reference type as the result; the expectation is that 
<code>common_type</code> will return a non-reference type to which all of the types can be converted.
<p/>
Daniel: In addition to that it should be noted that without such a fix the definition of <code>std::unique_ptr</code>'s
<code>operator&lt;</code> in 20.3.1.6 <a href="https://wg21.link/unique.ptr.special">[unique.ptr.special]</a> (around p4) is also broken: In the most typical case 
(with default deleter), the determination of the common pointer type <em>CT</em> will instantiate 
<code>std::less&lt;<em>CT</em>&gt;</code> which can now be <code>std::less&lt;T*&amp;&amp;&gt;</code>, which will
<em>not</em> be the specialization of pointer types that guarantess a total order.
<p/>
Given the historic constext of <code>common_type</code> original specification, the proper resolution to me
seems to be using <code>std::decay</code> instead of <code>std::remove_reference</code>: 
</p>
<blockquote><pre>
template &lt;class T, class U&gt;
struct common_type&lt;T, U&gt; {
  typedef <ins>typename decay&lt;</ins>decltype(true ? declval&lt;T&gt;() : declval&lt;U&gt;())<ins>&gt;::type</ins> type;
};
</pre></blockquote>
<p>
At that time rvalues had no identity in this construct and rvalues of non-class types have no cv-qualification.
With this change we would ensure that
</p>
<blockquote><pre>
common_type&lt;int, int&gt;::type == common_type&lt;const int, const int&gt;::type == int
</pre></blockquote>
<p>
Note that this harmonizes with the corresponding heterogenous case, which has already the exact same effect:
</p>
<blockquote><pre>
common_type&lt;int, long&gt;::type == common_type&lt;const int, const long&gt;::type == long
</pre></blockquote>

<p><i>[2012-10-11 Daniel comments]</i></p>

<p>
While testing the effects of applying the proposed resolution I noticed that this will have the effect that the unary 
form of <code>common_type</code>, like
</p>
<blockquote><pre>
common_type&lt;int&gt;
</pre></blockquote>
<p>
is not symmetric to the n-ary form (n &gt; 1). This is unfortunate, because this difference comes especially to effect when 
<code>common_type</code> is used with variadic templates. As an example consider the following <code>make_array</code> template:
</p>
<blockquote><pre>
#include &lt;array>
#include &lt;type_traits>
#include &lt;utility>

template&lt;class... Args&gt;
std::array&lt;typename std::common_type&lt;Args...&gt;::type, sizeof...(Args)&gt;
make_array(Args&amp;&amp;... args)
{
  typedef typename std::common_type&lt;Args...&gt;::type CT;
  return std::array&lt;CT, sizeof...(Args)&gt;{static_cast&lt;CT&gt;(std::forward&lt;Args&gt;(args))...};
}

int main()
{
  auto a1 = make_array(0); // OK: std::array&lt;int, 1&gt;
  auto a2 = make_array(0, 1.2); // OK: std::array&lt;double, 2&gt;
  auto a3 = make_array(5, true, 3.1415f, 'c'); // OK: std::array&lt;float, 4&gt;

  int i = 0;
  auto a1b = make_array(i); // <span style="color:#C80000;font-weight:bold">Error, attempt to form std::array&lt;int&amp;, 1&gt;</span>

  auto a2b = make_array(i, 1.2); // OK: std::array&lt;double, 2&gt;
  auto a2c = make_array(i, 0); // OK: std::array&lt;int, 2&gt;
}
</pre></blockquote>
<p>
The error for <code>a1b</code> <em>only</em> happens in the unary case and it is easy that it remains unnoticed
during tests. You cannot explain that reasonably to the user here.
<p/>
Of-course it is possible to fix that in this example by applying <code>std::decay</code> to the result of the 
<code>std::common_type</code> deduction. But if this is necessary here, I wonder why it should also be applied to 
the binary case, where it gives the wrong illusion of a complete type decay? The other way around: Why is 
<code>std::decay</code> not also applied to the unary case as well?
<p/>
This problem is not completely new and was already observed for the original <code>std::common_type</code> specification. 
At this time the <code>decltype</code> rules had a similar asymmetric effect when comparing
</p>
<blockquote><p>
<code>std::common_type&lt;const int, const int&gt;::type</code> (equal to '<code>int</code>' at this time)
</p></blockquote>
<p>
with:
</p>
<blockquote><p>
<code>std::common_type&lt;const int&gt;::type</code> (equal to '<code>const int</code>')
</p></blockquote>
<p>
and I wondered whether the unary form shouldn't also perform the same "decay" as the n-ary form.
<p/>
This problem makes me think that the current resolution proposal might not be ideal and I expect
differences in implementations (for those who consider to apply this proposed resolution already). I
see at least three reasonable options:
</p>
<ol>
<li><p>Accept the current wording suggestion for LWG 2141 as it is and explain that to users.</p></li>
<li><p>Keep <code>std::common_type</code> as currently specified in the Standard and tell users to use
<code>std::decay</code> where needed. Also fix other places in the library, e.g. the comparison
functions of <code>std::unique_ptr</code> or a most of the time library functions.</p></li>
<li><p>Apply <code>std::decay</code> also in the unary specialization of <code>std::common_type</code> with
the effect that <code>std::common_type&lt;const int&amp;&gt;::type</code> returns <code>int</code>.</p></li>
</ol>

<p><i>[2012-10-11 Marc Glisse comments]</i></p>

<p>
If we are going with decay everywhere, I wonder whether we should also decay in the 2-argument version <em>before</em> 
and not only <em>after</em>. So if I specialize <code>common_type&lt;mytype, double&gt;</code>, 
<code>common_type&lt;const mytype, volatile double&amp;&gt;</code> would automatically work.
</p>

<p><i>[2012-10-11 Daniel provides wording for bullet 3 of his list:]</i></p>


<ol style="list-style-type:upper-alpha">
<li><p>Change 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a> p3 as indicated:</p>
<blockquote><pre>
template &lt;class T&gt;
struct common_type&lt;T&gt; {
  typedef <ins>typename decay&lt;</ins>T<ins>&gt;::type</ins> type;
};

template &lt;class T, class U&gt;
struct common_type&lt;T, U&gt; {
  typedef <ins>typename decay&lt;</ins>decltype(true ? declval&lt;T&gt;() : declval&lt;U&gt;())<ins>&gt;::type</ins> type;
};
</pre></blockquote>
</li>
</ol>

<p><i>[2013-03-15 Issues Teleconference]</i></p>

<p>
Moved to Review.
</p>
<p>
Want to carefully consider the effect of <code>decay</code> vs. <code>remove_reference</code> with respect
to constness before adopting, although this proposed resolution stands for review in Bristol.
</p>

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


<p>Previous wording:</p>

<blockquote class="note"> 
<p>This wording is relative to N3376.</p>

<ol><li><p>In 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a> p3, change the <code>common_type</code> definition to</p>
<blockquote><pre>
template &lt;class T, class U&gt;
struct common_type&lt;T, U&gt; {
  typedef <ins>typename decay&lt;</ins>decltype(true ? declval&lt;T&gt;() : declval&lt;U&gt;())<ins>&gt;::type</ins> type;
};
</pre></blockquote>
</li>
</ol>
</blockquote>

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


<p>Move to Ready</p>

<p><i>[2013-09-29, Chicago]</i></p>


<p>Accepted for the working paper</p>



<p id="res-2141"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3485.</p>

<ol>
<li><p>Change 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a> p3 as indicated:</p>
<blockquote><pre>
template &lt;class T&gt;
struct common_type&lt;T&gt; {
  typedef <ins>typename decay&lt;</ins>T<ins>&gt;::type</ins> type;
};

template &lt;class T, class U&gt;
struct common_type&lt;T, U&gt; {
  typedef <ins>typename decay&lt;</ins>decltype(true ? declval&lt;T&gt;() : declval&lt;U&gt;())<ins>&gt;::type</ins> type;
};
</pre></blockquote>
</li>
</ol>





</body>
</html>
