<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 355: Operational semantics for a.back()</title>
<meta property="og:title" content="Issue 355: Operational semantics for a.back()">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue355.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#CD1">CD1</a> status.</em></p>
<h3 id="355"><a href="lwg-defects.html#355">355</a>. Operational semantics for a.back()</h3>
<p><b>Section:</b> 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Yaroslav Mironov <b>Opened:</b> 2002-01-23 <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#sequence.reqmts">active issues</a> in [sequence.reqmts].</p>
<p><b>View all other</b> <a href="lwg-index.html#sequence.reqmts">issues</a> in [sequence.reqmts].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>

<p>Table 68 &quot;Optional Sequence Operations&quot; in 23.1.1/12
specifies operational semantics for &quot;a.back()&quot; as
&quot;*--a.end()&quot;, which may be ill-formed <i>[because calling
operator-- on a temporary (the return) of a built-in type is
ill-formed]</i>, provided a.end() returns a simple pointer rvalue
(this is almost always the case for std::vector::end(), for
example). Thus, the specification is not only incorrect, it
demonstrates a dangerous construct: &quot;--a.end()&quot; may
successfully compile and run as intended, but after changing the type
of the container or the mode of compilation it may produce
compile-time error. </p>



<p id="res-355"><b>Proposed resolution:</b></p>
<p>Change the specification in table 68 &quot;Optional Sequence
Operations&quot; in 23.1.1/12 for &quot;a.back()&quot; from</p>


<blockquote><pre>
*--a.end()
</pre></blockquote>

<p>to</p>

<blockquote><pre>
  { iterator tmp = a.end(); --tmp; return *tmp; }
</pre></blockquote>

<p>and the specification for "a.pop_back()" from</p>

<blockquote><pre>
a.erase(--a.end())
</pre></blockquote>

<p>to</p>

<blockquote><pre>
  { iterator tmp = a.end(); --tmp; a.erase(tmp); }
</pre></blockquote>

<p><i>[Cura&ccedil;ao: LWG changed PR from &quot;{ X::iterator tmp =
a.end(); return *--tmp; }&quot; to &quot;*a.rbegin()&quot;, and from
&quot;{ X::iterator tmp = a.end(); a.erase(--tmp); }&quot; to
&quot;a.erase(rbegin())&quot;.]</i></p>


<p><i>[There is a second possible defect; table 68 &quot;Optional
Sequence Operations&quot; in the &quot;Operational Semantics&quot;
column uses operations present only in the &quot;Reversible
Container&quot; requirements, yet there is no stated dependency
between these separate requirements tables. Ask in Santa Cruz if the
LWG would like a new issue opened.]</i></p>


<p><i>[Santa Cruz: the proposed resolution is even worse than what's in
  the current standard: erase is undefined for reverse iterator.  If
  we're going to make the change, we need to define a temporary and
  use operator--.  Additionally, we don't know how prevalent this is:
  do we need to make this change in more than one place?  Martin has
  volunteered to review the standard and see if this problem occurs
  elsewhere.]</i></p>


<p><i>[Oxford: Matt provided new wording to address the concerns raised
  in Santa Cruz.  It does not appear that this problem appears
  anywhere else in clauses 23 or 24.]</i></p>


<p><i>[Kona: In definition of operational semantics of back(), change
"*tmp" to "return *tmp;"]</i></p>







</body>
</html>
