Movatterモバイル変換


[0]ホーム

URL:



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

940.std::distance

Section: 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 getfromfirst tolast.

IMO the part " or decrements" is in contradiction to p. 5 which says

Requires:last shall be reachable fromfirst.

because "reachable" is defined in 24.3.4[iterator.concepts]/7 as

An iteratorj is called reachable from an iteratori if and only ifthere is a finite sequence of applications of the expression++i that 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 incrementsor decrementsneeded to get fromfirst tolast.

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 theadvance() 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 toNAD EditorialResolved. Rationale added below.

Rationale:

Solved byN3066.

Proposed resolution:

  1. 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)
  2. 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:IfInputIterator meets the requirementsof random access iterator then returns(last - first),otherwiseRreturns the number of incrementsor decrements needed to get fromfirst tolast.

    5Requires:IfInputIterator meets the requirementsof random access iterator thenlast shall be reachable fromfirst orfirst shall be reachable fromlast,otherwiselast shall be reachable fromfirst.


[8]ページ先頭

©2009-2026 Movatter.jp