Title
The meaning of operator + in the description of the algorithms
Status
open
Section
[algorithms]
Submitter
Nikolay Ivchenkov

Created on 2012-08-01.00:00:00 last changed 78 months ago

Messages

Date: 2018-06-12.02:06:47

Proposed resolution:

This wording is relative to N4606.

  1. Change [algorithms.general] around p12 as indicated:

    -12- In the description of the algorithms operators + and - are used for some of the iterator categories for which they do not have to be defined. In these cases the semantics of a+n is the same as that of

    X tmp = a;
    advance(tmp, n);
    return tmp;
    

    when X meets the input iterator requirements ([input.iterators]), otherwise it is the same as that of

    X tmp = a;
    for (auto i = n; i; ++tmp, (void) --i) 
      *tmp = Expr(i); 
    return tmp;
    

    where Expr(i) denotes the (n-i)th expression that is assigned to for the corresponding algorithm; and that of b-a is the same as of

    return distance(a, b);
    
Date: 2018-06-12.02:06:47

[ 2018-06 Rapperswil Wednesday issues processing ]

Status to Open

Date: 2014-06-07.00:00:00

[ 2014-06-07 Daniel comments and provides wording ]

The specification for output iterators is somewhat tricky, because here a sequence of increments is required to be combined with intervening assignments to the dereferenced iterator. I tried to respect this fact by using a conceptual assignment operation as part of the specification.

Another problem in the provided as-if-code is the question which requirements are imposed on n. Unfortunately, the corresponding function advance is completely underspecified in this regard, so I couldn't borrow wording from it. We cannot even assume here that n is the difference type of the iterator, because for output iterators there is no requirements for this associated type to be defined. The presented wording attempts to minimize assumptions, but still can be considered as controversial.

Date: 2012-08-01.00:00:00

According to [algorithms.general]/12,

In the description of the algorithms operators + and - are used for some of the iterator categories for which they do not have to be defined. In these cases the semantics of a+n is the same as that of

X tmp = a;
advance(tmp, n);
return tmp;

There are several places where such operator + is applied to an output iterator — for example, see the description of std::copy:

template<class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last,
                    OutputIterator result);

-1- Effects: Copies elements in the range [first,last) into the range [result,result + (last - first)) starting from first and proceeding to last. For each non-negative integer n < (last - first), performs *(result + n) = *(first + n).

std::advance is not supposed to be applicable to output iterators, so we need a different method of description.

See also message c++std-lib-32908.

History
Date User Action Args
2018-06-12 02:06:47adminsetmessages: + msg9898
2018-06-12 02:06:47adminsetstatus: new -> open
2014-06-07 15:43:22adminsetmessages: + msg7004
2014-06-07 15:43:22adminsetmessages: + msg7003
2012-08-01 00:00:00admincreate