<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2182: Container::[const_]reference types are misleadingly specified</title>
<meta property="og:title" content="Issue 2182: Container::[const_]reference types are misleadingly specified">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2182.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="2182"><a href="lwg-defects.html#2182">2182</a>. <code>Container::[const_]reference</code> types are misleadingly specified</h3>
<p><b>Section:</b> 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2012-08-20 <b>Last modified:</b> 2017-07-05</p>
<p><b>Priority: </b>0
</p>
<p><b>View other</b> <a href="lwg-index-open.html#container.requirements.general">active issues</a> in [container.requirements.general].</p>
<p><b>View all other</b> <a href="lwg-index.html#container.requirements.general">issues</a> in [container.requirements.general].</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>
According to Table 96 (Container requirements) the return type of <code>X::reference</code> and
<code>X::const_reference</code> is "lvalue of <code>T</code>" and "<code>const</code> lvalue of <code>T</code>",
respectively. This does not make much sense, because an lvalue is an expression category, not a type.
It could also refer to an expression that has a type, but this doesn't make sense either in this
context, because obviously <code>X::[const_]reference</code> are intended to refer to types. 
<p/>
Given the fact that <code>vector&lt;bool&gt;</code> has no real reference type for <code>X::[const_]reference</code> 
and this definition presumably is intended to cover such situations as well, one might think that the wording is
just a sloppy form of "type that represents a [const] lvalue of <code>T</code>". But this is also problematic,
because basically all proxy reference expressions are rvalues.
<p/>
It is unclear what the intention is. A straightward way of fixing this wording could make
<code>X::[const_]reference</code> identical to <code>[const] T&amp;</code>. This holds for all Library containers
except for <code>vector&lt;bool&gt;</code>.
<p/>
Another way of solving this definition problem would be to impose a requirement that holds for both
references and reference-like proxies. Both <code>X::reference</code> and <code>X::const_reference</code>
would need to be convertible to <code>const T&amp;</code>. Additionally <code>X::reference</code> would need to
support for a mutable container an assignment expression of the form 
<code>declval&lt;X::reference&gt;() = declval&lt;T&gt;()</code> (this presentation intentionally does not require 
<code>declval&lt;X::reference<span style="color:#C80000;font-weight:bold">&amp;</span>&gt;() = declval&lt;T&gt;()</code>).
<p/>
Further, the Table 96 does not impose any relations between <code>X::reference</code> and <code>X::const_reference</code>.
It seems that at least <code>X::reference</code> needs to be convertible to <code>X::const_reference</code>.
<p/>
A related question is whether <code>X::reference</code> is supposed to be a mutable reference-like type,
irrespective of whether the container is an immutable container or not. The way, type <code>match_results</code>
defines <code>reference</code> identical to <code>const_reference</code> indicates one specific interpretation (similarly,
the <code>initializer_list</code> template also defines member type <code>reference</code> equal to <code>const value_type&amp;</code>).
Note that this can be a different decision as that for <code>iterator</code> and <code>const_iterator</code>,
e.g. for sets the type <code>X::reference</code> still is a mutable reference, even though <code>iterator</code>
is described as constant iterator.
<p/>
The proposed resolution is incomplete in regard to the last question.
</p>

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

<p>
Moved to Review.
</p>
<p>
Alisdair notes that this looks like wording in the right direction.  Wonders about congruence of these
typedefs and the similar ones for iterators.
</p>

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

<p>
Moved to Ready.
</p>
<p>
Consensus that the requirements should require real references, just like iterators, as containers are
required to support at least ForwardIterators, which have the same restriction on references.
</p>

<p>
Matt will file a new issue for some additional concerns with regex <code>match_results</code>.
</p>

<p><i>[2014-02-10, Daniel comments]</i></p>


<p>
The new issue opened by Matt is LWG <a href="lwg-defects.html#2306" title="match_results::reference should be value_type&amp;, not const value_type&amp; (Status: C++14)">2306</a><sup><a href="https://cplusplus.github.io/LWG/issue2306" title="Latest snapshot">(i)</a></sup>.
</p>

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

<p>
Issue should have been Ready in pre-meeting mailing.
</p>



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

<ol>
<li><p>Change Table 96 &mdash; "Container requirements" as indicated:</p>

<table border="1">
<caption>Table 96 &mdash; Container requirements</caption>

<tr>
<th>Expression</th>
<th>Return type</th>
<th>Operational<br/>Semantics</th>
<th>Assertion/note<br/>pre-/post-condition</th>
<th>Complexity</th>
</tr> 

<tr>
<td>
<code>X::reference</code>
</td>
<td>
<del>lvalue of </del><code>T<ins>&amp;</ins></code>
</td>
<td>
&nbsp;
</td>
<td>
</td>
<td>
compile time
</td>
</tr>

<tr>
<td>
<code>X::const_reference</code>
</td>
<td>
<del>const lvalue of</del><ins><code>const</code></ins> <code>T<ins>&amp;</ins></code>
</td>
<td>
&nbsp;
</td>
<td>
</td>
<td>
compile time
</td>
</tr>

</table>

</li>
</ol>





</body>
</html>
