ISO/ IEC JTC1/SC22/WG21 N4442

Default argument for second parameter of std::advance (Rev. 1)

Document number: N4442
Revises: N4369

Project: Programming Language C++, Library Evolution Working Group

Date: 2015-04-09
Reply-To: Vladimir Grigoriev (vlad.moscow@mail.ru)

Revision history.

In the preceding version of the document there was
a type in the suggested declaration of std::advance.
Now the function declaration is updated.

Preface. 

Functions that perform operations over iterators
like std::prev and std::next have a default argument 
for their second parameter. Thus it is redundant to specify
for example 

auto it2 = std::next( it1, 1 ); 

instead of simple 

auto it2 = std::next( it1 ). 

The last record is more clear and more
expressive. The reader need not to investigate 
what value was specified for second parameter.
He understands that the result of the operation is
the iterator that points to the next position in the 
sequence after the current position
 pointed to by the iterator specified as the argument.

And only function std::advance does not have the default
argument for its second parameter though the record

std::advance( it1 );

is not less expressive than record

auto it2 = std::next( it1 ). 

Thus it would be more logical consistent if the function
 also had the default argument equal to 1 for the
second parameter.


Changes to the C++ Standard.

Change the function declarations by means of including
default argument equal to 1 for the second parameter.
 
// 24.4.4, iterator operations:
template <class InputIterator, 
          class Distance = typename iterator_traits<InputIterator>::difference_type>
void advance(InputIterator& i, Distance n = 1);


24.4.4 Iterator operations [iterator.operations]
1 Since only random access iterators provide + and - operators, the library provides two function templates
advance and distance. These function templates use + and - for random access iterators (and are, therefore,
constant time for them); for input, forward and bidirectional iterators they use ++ to provide linear time
implementations.
template <class InputIterator, class Distance>
void advance(InputIterator& i, Distance n = 1);
2 Requires: n shall be negative only for bidirectional and random access iterators.
3 Effects: Increments (or decrements for negative n) iterator reference i by n.