This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofResolved status.
std::distanceSection: 24.4.3[iterator.operations]Status:ResolvedSubmitter: ThomasOpened: 2008-12-14Last modified: 2016-01-28
Priority:Not Prioritized
View otheractive issues in [iterator.operations].
View all otherissues in [iterator.operations].
View all issues withResolved status.
Discussion:
Addresses UK 270
Regarding thestd::distance - function, 24.4.3[iterator.operations]p.4 says:
Returns the number of increments or decrements needed to get from first to last.
This sentence is completely silent about the sign of the return value.24.4.3[iterator.operations] p.1 gives more information about theunderlying operations, but again no inferences about the sign can be made.Strictly speaking, that is taking that sentence literally, I think thissentence even implies a positive return value in all cases, as thenumber of increments or decrements is clearly a ratio scale variable,with a natural zero bound.
Practically speaking, my implementations did what common sense andknowledge based on pointer arithmetic forecasts, namely a positive signfor increments (that is, going fromfirst tolast byoperator++), and anegative sign for decrements (going fromfirst tolast byoperator--).
Here are my two questions:
First, is that paragraph supposed to be interpreted in the way what Icalled 'common sense', that is negative sign for decrements ? I amfairly sure that's the supposed behavior, but a double-check here inthis group can't hurt.
Second, is the present wording (2003 standard version - no idea aboutthe draft for the upcoming standard) worth an edit to make it a bit moresensible, to mention the sign of the return value explicitly ?
[Daniel adds:]
My first thought was that resolution204(i) would already cover theissue report, but it seems that current normative wording is incontradiction to that resolution:
Referring toN2798,24.4.3[iterator.operations]/ p.4 says:
Effects: Returns the number of increments or decrements needed to getfrom
firsttolast.IMO the part " or decrements" is in contradiction to p. 5 which says
Requires:
lastshall be reachable fromfirst.because "reachable" is defined in 24.3.4[iterator.concepts]/7 as
An iterator
jis called reachable from an iteratoriif and only ifthere is a finite sequence of applications of the expression++ithat makesi == j.[..]Here is wording that would be consistent with this definition of "reachable":
Change 24.4.3[iterator.operations] p4 as follows:
Effects: Returns the number of increments
or decrementsneeded to get fromfirsttolast.
Thomas adds more discussion and an alternative view pointhere.
[Summit:]
The proposed wording below was verbally agreed to. Howard provided.
[Batavia (2009-05):]
Pete reports that a recent similar change has been madefor the
advance()function.We agree with the proposed resolution.Move to Tentatively Ready.
[2009-07 Frankfurt]
Moved from Tentatively Ready to Open only because the wording needs to betweaked for concepts removal.
[2009-07 Frankfurt:]
Leave Open pending arrival of a post-Concepts WD.
[2009-10-14 Daniel provided de-conceptified wording.]
[2009-10 Santa Cruz:]
Move to Ready, replacing the Effects clause in the proposed wording with"If InputIterator meets the requirements of random access iterator thenreturns (last - first), otherwise returns the number of incrementsneeded to get from first to list.".
[2010 Pittsburgh:]
Moved to
NAD EditorialResolved. Rationale added below.
Rationale:
Solved byN3066.
Proposed resolution:
Change 24.3.5.7[random.access.iterators], Table 105 as indicated [This change is notessential but it simplifies the specification] for the row with expression "b - a"and the column Operational semantics:
(a < b) ?distance(a,b): -distance(b,a)
Change 24.4.3[iterator.operations]/4+5 as indicated:
template<class InputIterator> typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);4Effects:If
InputIteratormeets the requirementsof random access iterator then returns(last - first),otherwiseRreturns the number of incrementsor decrementsneeded to get fromfirsttolast.5Requires:If
InputIteratormeets the requirementsof random access iterator thenlastshall be reachable fromfirstorfirstshall be reachable fromlast,otherwiselastshall be reachable fromfirst.