Implementation of 'counted_iterator'