<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1034: Clarify generality of Container Requirement tables</title>
<meta property="og:title" content="Issue 1034: Clarify generality of Container Requirement tables">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1034.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++11">C++11</a> status.</em></p>
<h3 id="1034"><a href="lwg-defects.html#1034">1034</a>. Clarify generality of Container Requirement tables</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++11">C++11</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2009-03-12 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</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++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>

<p><b>Addresses UK 222 [CD1]</b></p>

<p>
It is not clear what purpose the Requirement tables serve in the
Containers clause. Are they the definition of a library Container? Or
simply a conventient shorthand to factor common semantics into a single
place, simplifying the description of each subsequent container? This
becomes an issue for 'containers' like <code>array</code>, which does not meet the
default-construct-to-empty requirement, or <code>forward_list</code> which does not
support the size operation. Are these components no longer containers?
Does that mean the remaining requirements don't apply? Or are these
contradictions that need fixing, despite being a clear design decision?
</p>

<p>
Recommend:
</p>

<p>
Clarify all the tables in 23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a> are
there as a convenience for documentation, rather than a strict set of
requirements. Containers should be allowed to relax specific
requirements if they call attention to them in their documentation. The
introductory text for <code>array</code> should be expanded to mention a
default constructed <code>array</code> is not empty, and
<code>forward_list</code> introduction should mention it does not provide
the required <code>size</code> operation as it cannot be implemented
efficiently.
</p>

<p><i>[
Summit:
]</i></p>


<blockquote><p>
Agree in principle.
</p></blockquote>

<p><i>[
2009-07 post-Frankfurt:
]</i></p>


<blockquote><p>
We agree in principle, but we have a timetable. This group feels that
the issue should be closed as NAD unless a proposed resolution is
submitted prior to the March 2010 meeting.
</p></blockquote>

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


<blockquote><p>
Looked at this and still intend to close as NAD in March
2010 unless there is proposed wording that we like.
</p></blockquote>

<p><i>[
2010-02-02 Nicolai M. Josuttis updates proposed wording and adds:
]</i></p>


<blockquote>
<p>
I just came across issue #1034 (response to UK 222),
which covers the role of container requirements.
The reason I found this issue was that I am wondering why
<code>array&lt;&gt;</code> is specified to be a sequence container.
For me, currently, this follows from
Sequence containers 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a>
saying:
</p>
<blockquote><p>
The library provides five basic kinds of sequence containers: <code>array</code>,
<code>vector</code>, <code>forward_list</code>, <code>list</code>, and <code>deque</code>. while
later on in Table 94 "Sequence container requirements" are defined.
</p></blockquote>

<p>
IMO, you can hardly argue that this is NAD.
We MUST say somewhere that either array is not a sequence container
or does not provide all operations of a sequence container
(even not all requirements of a container in general).
</p>
<p>
Here is the number of requirements <code>array&lt;&gt;</code> does not meet
(AFAIK):
</p>
<p>
general container requirements:
</p>
<ul>
<li>
a default constructed <code>array</code> is not empty
</li>
<li>
<code>swap</code> has no constant complexity
</li>
</ul>

<p>
 Note also that <code>swap</code> not only has linear complexity
 it also invalidates iterators (or to be more precise,
 assigns other values to the elements), which
 is different from the effect swap has for other containers.
 For this reason, I must say that i tend to propose to
 remove <code>swap()</code> for <code>arrays</code>.
 </p>

<p>
sequence container requirements:
</p>

<ul>
<li>
There is no constructor and assignment for a range
</li>
<li>
There is no constructor and assignment for <code>n</code> copies of <code>t</code>
</li>
<li>
 There are no <code>emplace</code>, <code>insert</code>, <code>erase</code>, <code>clear</code>,
 <code>assign</code> operations
</li>
</ul>

<p>
In fact, out of all sequence container requirements <code>array&lt;&gt;</code> only
provides the following operations:
from sequence requirements (Table 94):
</p>
<blockquote><pre>
X(il);
a = il;
</pre></blockquote>
<p>
and from optional requirements (Table 95):
</p>
<blockquote><pre>
[], at(), front(), back()
</pre></blockquote>
<p>
This is almost nothing!
</p>

<p>
Note in addition, that due to the fact that
<code>array</code> is an aggregate and not a container with
<code>initializer_lists</code>
a construction or assignment with an initializer list is valid
for all sequence containers but not valid for array:
</p>

<blockquote><pre>
vector&lt;int&gt;  v({1,2,3});   // OK
v = {4,5,6};               // OK

array&lt;int,3&gt; a({1,2,3});   // Error
array&lt;int,3&gt; a = {1,2,3};  // OK
a = {4,5,6};               // Error
</pre></blockquote>

<p>
BTW, for this reason, I am wondering, why <code>&lt;array&gt;</code> includes
<code>&lt;initializer_list&gt;</code>.
</p>

<p>
IMO, we can't really say that <code>array</code> is a sequence container.
<code>array</code> is special.
As the solution to this issue seemed to miss some proposed wording
where all could live with, let me try to suggest some.
</p>

</blockquote>

<p><i>[
2010-02-12 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>


<p><i>[
2010 Pittsburgh:  Ok with move to Ready except for "OPEN:" part.
]</i></p>




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

<p><i>In Sequence containers 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a> modify paragraph 1 as 
indicated: </i> </p>
<blockquote>
  <p>1 A sequence container organizes a finite set of objects, all of the same 
  type, into a strictly linear arrangement. The library provides <del>five</del>
  <ins>four</ins> basic kinds of sequence containers: <del><code>array</code>,</del>
  <code>vector</code>, <code>forward_list</code>, <code>list</code>, and <code>deque</code>.
  <ins>In addition, <code>array</code> is provided as a sequence container that 
  only provides limited sequence operations because it has a fixed number of 
  elements.</ins> <del>It</del> <ins>The library</ins> also provides container adaptors that make it easy to 
  construct abstract data types, such as <code>stack</code>s or <code>queue</code>s, out 
  of the basic sequence container kinds (or out of other kinds of sequence 
  containers that the user might define). </p>
</blockquote>
<p><i>Modify paragraph 2 as follows (just editorial): </i> </p>
<blockquote>
  <p>2 The <del>five basic</del> sequence 
  containers offer the programmer different complexity trade-offs and should be 
  used accordingly. <code>vector</code> or <code>array</code> is the type of sequence 
  container that should be used by default. <code>list</code> or <code>forward_list</code> 
  should be used when there are frequent insertions and deletions from the 
  middle of the sequence. <code>deque</code> is the data structure of choice when 
  most insertions and deletions take place at the beginning or at the end of the 
  sequence. </p>
</blockquote>
<p><i>In Class template array 23.3.3 <a href="https://wg21.link/array">[array]</a> modify paragraph 3 as indicated:
</i> </p>
<blockquote>
  <p>3 <del>Unless otherwise specified, all <code>array</code> operations are as 
  described in 23.2.</del> <ins>An array satisfies all of the requirements of a 
  container and of a reversible container (given in two tables in 23.2 <a href="https://wg21.link/container.requirements">[container.requirements]</a>) 
  except that a default constructed <code>array</code> is not empty, <code>swap</code> 
  does not have constant complexity, and <code>swap</code> may throw exceptions. An <code>array</code> satisfies some of the requirements of a 
  sequence container (given in 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a>).</ins> Descriptions are 
  provided here only for operations on <code>array</code> that are not described
  <del>in that Clause</del> <ins>in one of these tables</ins> or for operations 
  where there is additional semantic information. </p>
</blockquote>
<p><i>In array specialized algorithms 23.3.3.4 <a href="https://wg21.link/array.special">[array.special]</a> add to the 
specification of <code>swap()</code>: </i> </p>
<blockquote>
  <pre>template &lt;class T, size_t N&gt; void swap(array&lt;T,N&gt;&amp; x, array&lt;T,N&gt;&amp; y);
</pre>
  <blockquote>
    <p>1 <i>Effects:</i> ... </p>
    <p><ins><i>Complexity:</i> Linear in <code>N</code>. </ins></p>
  </blockquote>
</blockquote>









</body>
</html>
