<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2042: Comparing forward_list::before_begin() to forward_list::end()</title>
<meta property="og:title" content="Issue 2042: Comparing forward_list::before_begin() to forward_list::end()">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2042.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="2042"><a href="lwg-defects.html#2042">2042</a>. Comparing <code>forward_list::before_begin()</code> to <code>forward_list::end()</code></h3>
<p><b>Section:</b> 23.3.7.3 <a href="https://wg21.link/forward.list.iter">[forward.list.iter]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Joe Gottman <b>Opened:</b> 2011-03-13 <b>Last modified:</b> 2023-02-07</p>
<p><b>Priority: </b>Not Prioritized
</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>For an object <code>c</code> of type <code>forward_list&lt;X, Alloc&gt;</code>, the iterators
<code>c.before_begin()</code> and <code>c.end()</code> are part of the same underlying sequence,
so the expression <code>c.before_begin() == c.end()</code>  must be well-defined.
But the standard says nothing about what the result of this expression
should be.  The forward iterator requirements says no dereferenceable
iterator is equal to a non-dereferenceable iterator and that two
dereferenceable iterators are equal if and only if they point to the
same element.  But since <code>before_begin()</code> and <code>end()</code> are both
non-dereferenceable, neither of these rules applies.
</p>

<p>
Many <code>forward_list</code> methods, such as <code>insert_after()</code>, have a
precondition that the iterator passed to them must not be equal to
<code>end()</code>. Thus, user code might look like the following:
</p>
<blockquote><pre>
void foo(forward_list&lt;int&gt;&amp; c, forward_list&lt;int&gt;::iterator it)
{
  assert(it != c.end());
  c.insert_after(it, 42);
}
</pre></blockquote>

<p>
Conversely, <code>before_begin()</code> was specifically designed to be used with
methods like <code>insert_after()</code>, so if <code>c.before_begin()</code> is passed to 
this function the assertion must not fail.
</p>

<p><i>[2011-03-14: Daniel comments and updates the suggested wording]</i></p>


<p>The suggested wording changes are necessary but not sufficient. Since there
does not exist an equivalent semantic definition of <code>cbefore_begin()</code> as
we have for <code>cbegin()</code>, this still leaves the question open whether
the normative remark applies to <code>cbefore_begin()</code> as well. A simple fix
is to define the operational semantics of <code>cbefore_begin()</code> in terms of
<code>before_begin()</code>.</p>

<p><i>[2011-03-24 Madrid meeting]</i></p>


<p>
General agreement that this is a serious bug.
<p/>
Pablo: Any objections to moving 2042 to Immediate?
<p/>
No objections. 
</p>



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

<p>Add to the definition of <code>forward_list::before_begin()</code>  [forwardlist.iter] 
the following:</p>

<blockquote><pre>
iterator before_begin();
const_iterator before_begin() const;
const_iterator cbefore_begin() const;
</pre><blockquote><p>
-1- <i>Returns</i>: A non-dereferenceable iterator that, when incremented, is equal to the iterator returned by <code>begin()</code>.
<p/>
<ins>-?- <i>Effects</i>: <code>cbefore_begin()</code> is equivalent to <code>const_cast&lt;forward_list const&amp;>(*this).before_begin()</code>.</ins>
<p/>
<ins>-?- <i>Remarks</i>: <code>before_begin() == end()</code> shall equal false.</ins>
</p></blockquote></blockquote>





</body>
</html>
