stl - How to implement "dereference and post-increment" for input iterator in C++? -


requirements inputiterator include *i++ equivalent expression being

value_type x = *i; ++i; return x; 

how can 1 declare such operator without implementing standard post-increment i++ returning non-void value (which inputiterators not required do)?

you may use proxy post increment:

#include <iostream>  class input_iterator {     private:     class post_increment_proxy     {         public:         post_increment_proxy(int value) : value(value) {}         int operator * () const { return value; }          private:         int value;     };      public:     post_increment_proxy operator ++ (int) {         post_increment_proxy result{value};         ++value;         return result;     }      private:     int value = 0; };   int main() {     input_iterator i;     std::cout << *i++ << '\n';     std::cout << *i++ << '\n';     std::cout << *i++ << '\n'; } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -