[iterator] Constness of counting_iterator

Hi all! I was wondering why the dereference operator of counting_iterator<T> returns a constant reference to T instead of a reference to T. Naively I would expect counting_iterator<T>::reference to be T& and counting_iterator<const T>::reference to be const T&. I suppose however that there was good reason to implement counting_iterator the way it is implemented. Could anyone kindly point this reason out to me? Regards Claas

on Fri Feb 01 2013, Claas <claas.koehler-AT-dlr.de> wrote:
Hi all!
I was wondering why the dereference operator of counting_iterator<T> returns a constant reference to T instead of a reference to T.
Naively I would expect counting_iterator<T>::reference to be T& and counting_iterator<const T>::reference to be const T&.
I suppose however that there was good reason to implement counting_iterator the way it is implemented. Could anyone kindly point this reason out to me?
Counting iterators are strictly constant iterators. Writing into a counting iterator would alter its position in the sequence. That doesn't seem compatible with the expected behavior of iterators that could be used by any algorithm. -- Dave Abrahams BoostPro Computing Software Development Training http://www.boostpro.com Clang/LLVM/EDG Compilers C++ Boost
participants (2)
-
Claas
-
Dave Abrahams