This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of CD1 status.

355. Operational semantics for a.back()

Section: 23.2.4 [sequence.reqmts] Status: CD1 Submitter: Yaroslav Mironov Opened: 2002-01-23 Last modified: 2016-01-28

Priority: Not Prioritized

View other active issues in [sequence.reqmts].

View all other issues in [sequence.reqmts].

View all issues with CD1 status.

Discussion:

Table 68 "Optional Sequence Operations" in 23.1.1/12 specifies operational semantics for "a.back()" as "*--a.end()", which may be ill-formed [because calling operator-- on a temporary (the return) of a built-in type is ill-formed], 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: "--a.end()" 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.

Proposed resolution:

Change the specification in table 68 "Optional Sequence Operations" in 23.1.1/12 for "a.back()" from

*--a.end()

to

  { iterator tmp = a.end(); --tmp; return *tmp; }

and the specification for "a.pop_back()" from

a.erase(--a.end())

to

  { iterator tmp = a.end(); --tmp; a.erase(tmp); }

[Curaçao: LWG changed PR from "{ X::iterator tmp = a.end(); return *--tmp; }" to "*a.rbegin()", and from "{ X::iterator tmp = a.end(); a.erase(--tmp); }" to "a.erase(rbegin())".]

[There is a second possible defect; table 68 "Optional Sequence Operations" in the "Operational Semantics" column uses operations present only in the "Reversible Container" 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.]

[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.]

[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.]

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