<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2313: tuple_size should always derive from integral_constant&lt;size_t, N&gt;</title>
<meta property="og:title" content="Issue 2313: tuple_size should always derive from integral_constant&lt;size_t, N&gt;">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2313.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="2313"><a href="lwg-defects.html#2313">2313</a>. <code>tuple_size</code> should always derive from <code>integral_constant&lt;size_t, N&gt;</code></h3>
<p><b>Section:</b> 22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2013-09-21 <b>Last modified:</b> 2017-07-05</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#tuple.helper">issues</a> in [tuple.helper].</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>
In 22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a>, the "primary template" is depicted as:
</p>
<blockquote><pre>
template &lt;class... Types&gt;
class tuple_size&lt;tuple&lt;Types...&gt; &gt;
  : public integral_constant&lt;size_t, sizeof...(Types)&gt; { };
</pre></blockquote>
<p>
However, 22.3.4 <a href="https://wg21.link/pair.astuple">[pair.astuple]</a>/1-2 and 23.3.3.7 <a href="https://wg21.link/array.tuple">[array.tuple]</a>/1-2 are underspecified, saying:
</p>
<blockquote><pre>
tuple_size&lt;pair&lt;T1, T2&gt; &gt;::value
</pre><blockquote>
<p>
<i>Returns:</i> Integral constant expression.
<p/>
<i>Value:</i> <code>2</code>.
</p></blockquote>
</blockquote>
<blockquote><pre>
tuple_size&lt;array&lt;T, N&gt; &gt;::value
</pre><blockquote>
<p>
<i>Return type:</i> integral constant expression.
<p/>
<i>Value:</i> <code>N</code>
</p></blockquote>
</blockquote>
<p>
They should be required to behave like the "primary template". This is more than a stylistic decision &mdash; it allows 
<code>tuple_size</code> to be passed to a function taking <code>integral_constant</code>.
<p/>
LWG <a href="lwg-defects.html#1118" title="tuple query APIs do not support cv-qualification (Status: C++11)">1118</a><sup><a href="https://cplusplus.github.io/LWG/issue1118" title="Latest snapshot">(i)</a></sup> noticed this underspecification, but instead of correcting it, the resolution changed 
22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a>/3 to require <code>tuple_size&lt;<i>cv</i> T&gt;</code> to derive from 
<code>integral_constant&lt;remove_cv&lt;decltype(TS::value)&gt;::type, TS::value&gt;</code>. This is unnecessarily overgeneralized.  
<code>tuple_size</code> is primarily for tuples, where it is required to be <code>size_t</code>, and it has been extended to handle 
pairs and arrays, which (as explained above) should also be guaranteed to be <code>size_t</code>. <code>tuple_size&lt;<i>cv</i> T&gt;</code> 
works with <i>cv</i>-qualified tuples, pairs, arrays, and user-defined types that also want to participate in the <code>tuple_size</code> 
system. It would be far simpler and perfectly reasonable to expect that user-defined types supporting the "tuple-like protocol" 
should have <code>tuple_sizes</code> of <code>size_t</code>.
</p>

<p><i>[Issaquah 2014-02-11: Move to Immediate]</i></p>




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

<ol>
<li><p>Edit 22.3.4 <a href="https://wg21.link/pair.astuple">[pair.astuple]</a>/1-2 as indicated:</p>

<blockquote><pre>
<del>tuple_size&lt;pair&lt;T1, T2&gt; &gt;::value</del>
<ins>template &lt;class T1, class T2&gt;
struct tuple_size&lt;pair&lt;T1, T2&gt;&gt;
  : integral_constant&lt;size_t, 2&gt; { };</ins>
</pre><blockquote>
<p>
<del>-1- <i>Returns:</i> Integral constant expression.</del>
<p/>
<del>-2- <i>Value:</i> <code>2</code>.</del>
</p></blockquote>
</blockquote>
</li>

<li><p>Edit 23.3.3.7 <a href="https://wg21.link/array.tuple">[array.tuple]</a>/1-2 as indicated:</p>

<blockquote><pre>
<del>tuple_size&lt;array&lt;T, N&gt; &gt;::value</del>
<ins>template &lt;class T, size_t N&gt;
struct tuple_size&lt;array&lt;T, N&gt;&gt;
  : integral_constant&lt;size_t, N&gt; { };</ins>
</pre><blockquote>
<p>
<del>-1- <i>Returns:</i> Integral constant expression.</del>
<p/>
<del>-2- <i>Value:</i> <code>N</code>.</del>
</p></blockquote>
</blockquote>
</li>

<li><p>Edit 22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a>/p1-p3 as indicated:</p>

<blockquote><pre>
<ins>template &lt;class T&gt; struct tuple_size;</ins>
</pre><blockquote>
<p>
<ins>-?- <i>Remarks:</i> All specializations of <code>tuple_size&lt;T&gt;</code> shall meet the <code>UnaryTypeTrait</code> 
requirements (21.3.2 <a href="https://wg21.link/meta.rqmts">[meta.rqmts]</a>) with a <code>BaseCharacteristic</code> of <code>integral_constant&lt;size_t, N&gt;</code> 
for some <code>N</code>.</ins>
</p>
</blockquote>
</blockquote>
<blockquote><pre>
template &lt;class... Types&gt;
struct tuple_size&lt;tuple&lt;Types...&gt; &gt;
  : integral_constant&lt;size_t, sizeof...(Types)&gt; { };
  
template &lt;size_t I, class... Types&gt;
class tuple_element&lt;I, tuple&lt;Types...&gt; &gt; {
public:
  typedef TI type;
};
</pre><blockquote>
<p>
-1- <i>Requires:</i> <code>I &lt; sizeof...(Types)</code>. The program is ill-formed if <code>I</code> is out of bounds.
<p/>
[&hellip;]
</p></blockquote>
</blockquote>
<blockquote><pre>
template &lt;class T&gt; class tuple_size&lt;const T&gt;;
template &lt;class T&gt; class tuple_size&lt;volatile T&gt;;
template &lt;class T&gt; class tuple_size&lt;const volatile T&gt;;
</pre><blockquote>
<p>
-3- Let <i>TS</i> denote <code>tuple_size&lt;T&gt;</code> of the <i>cv</i>-unqualified type <code>T</code>. Then each of the 
three templates shall meet the <code>UnaryTypeTrait</code> requirements (21.3.2 <a href="https://wg21.link/meta.rqmts">[meta.rqmts]</a>) with a <code>BaseCharacteristic</code> of
</p>
<blockquote><pre>
integral_constant&lt;<del>remove_cv&lt;decltype(TS::value)&gt;::type</del><ins>size_t</ins>, TS::value&gt;
</pre></blockquote>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
