Re: std::distance
On 15 Dez., 03:31, Tom <spamf...@nothing.com> wrote:
Regarding the std::distance - function, 24.3.4 / 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.3.4 / 1 gives more information about the underlying operations, but
again no inferences about the sign can be made.
Strictly speaking, that is taking that sentence literally, I think
this
sentence even implies a positive return value in all cases, as the
number of increments or decrements is clearly a ratio scale variable,
with a natural zero bound.
Practically speaking, my implementations did what common sense and
knowledge based on pointer arithmetic forecasts, namely a positive
sign
for increments (that is, going from first to last by operator++), and
a
negative sign for decrements (going from first to last by operator--).
Here are my two questions:
First, is that paragraph supposed to be interpreted in the way what I
called 'common sense', that is negative sign for decrements ? I am
fairly sure that's the supposed behavior, but a double-check here in
this group can't hurt.
I think the standard is quite clear in this point. Note that p.5 of
the mentioned
section has the following requirement:
"last must be reachable from first."
Now refer to [lib.iterator.requirements]/6, which says:
"An iterator j is called reachable from an iterator i if and only if
there is
a finite sequence of applications of the expression ++i that makes i
== j.
If j is reachable from i, they refer to the same container."
p.7 strengthens this position:
"Most of the library?s algorithmic templates that operate on data
structures
have interfaces that use ranges. A range is a pair of iterators that
designate
the beginning and end of the computation. A range [i, i) is an empty
range;
in general, a range [i, j) refers to the elements in the data
structure starting
with the one pointed to by i and up to but not including the one
pointed to
by j. Range [i, j) is valid if and only if j is reachable from i. The
result of the
application of functions in the library to invalid ranges is
undefined."
The most relevant part for this discussion are the last two sentences
of
this paragraph.
If this is not enough, further evidence comes from table 76. Note
that
the row corresponding to the expression b - a has the operational
semantic of
(a<b)? distance(a,b): -distance(b,a)
So, although we are in the domain of random access iterators, distance
is used in a manner to fulfill requirements which you seem to assume
do only apply for non-random access iterators.
Therefore is I see no other choice than a positive outcome of this
function, because the user would jump into undefined behavior,
if last would not be reachable from first. In other words, for every
random access iterator the requirement holds that last >= first. It
is pure convenience from your side, if you make arguments violating
this requirement valid input.
Second, is the present wording (2003 standard version - no idea about
the draft for the upcoming standard) worth an edit to make it a bit
more
sensible, to mention the sign of the return value explicitly ?
On a quick view I see no differences regarding this point.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]