c++ - How to tell advance() to use += operator on input iterators if they aren't random access -
consider input iterator join_iterator: iterates on concatenation of other ranges. calling ++i repeatedly can much slower simple i += n.
nevertheless, c++ code requires advancing iterator arbitrary amount uses std::advance, automatically resorts calling ++i when iterator isn't random-access.
(sadly, people use std::advance(i, n) instead of using std::advance; advance(i, n), can't supply advance iterator , rely on adl.)
on other hand, can't use + or += because input iterators don't have implement them.
so question is: how go supporting such scenario, when:
implementing such iterator?
using input iterator might have optimized
operator +=?
(note advance , + isn't scenario in matters -- distance , - has same problem.)
according c++11 §24.4.4,
since random access iterators provide + , - operators, library provides 2 function templates
advance,distance. these function templates use+,-random access iterators (and are, therefore, constant time them); input, forward , bidirectional iterators use ++ provide linear time implementations.
you should have define + , -, , specify std::random_access_iterator_tag. there no need specialize or overload std::advance.
Comments
Post a Comment