c++ - std::next with n > std::distance(it, c.end()) -


i not want use std::distance because calculate whole distance iterator end. need sure have n or more elements iterator end. i'm using next code:

if (std::next(it, n) != c.end()) // c std::multimap {     /// logic } 

everything great , working compiler (g++ (gcc) 4.8.3 20140911 (red hat 4.8.3-9)) have doubts. in documentation (cpprefenece.com && cplusplus.com) can not find information case when n > std::distance(it , c.end()) or other exceptional cases. so. code safe? or should write own nextifpossible?

according standard §24.4.4/p3 & p6 iterator operations [iterator.operations] (emphasis mine):

template <class inputiterator, class distance> constexpr void advance(inputiterator& i, distance n); 

2 requires: n shall negative bidirectional , random access iterators.

3 effects: increments (or decrements negative n) iterator reference n.

template <class inputiterator> constexpr inputiterator next(inputiterator x, typename std::iterator_traits<inputiterator>::difference_type n = 1); 

6 effects: equivalent to: advance(x, n); return x;

consequently, there's no bound checking , therefore may result in undefined behaviour if input n greater std::distance(it , c.end()).


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -