Opened 14 years ago

Closed 13 years ago

#255 closed defect (fixed)

Our iterators should use the special std base class iterator<>

Reported by: Markus Ringnér Owned by: Peter
Priority: minor Milestone: yat 0.4
Component: utility Version:
Keywords: Cc:

Description

We are setting a bunch of typedefs first in Iterator (and IteratorWeighted?)

typedef std::random_access_iterator_tag iterator_category; typedef double value_type; typedef size_t difference_type; typedef double* pointer; typedef double& reference;

which are the 5 definitions required by std. The standard library contains a special base class iterator<> that only sets the five definitions:

class Iterator

: public std::iterator<std::random_access_iterator_tag,double,size_t,double*, double&>

{ ... };

The last 3 arguments are optional and have default values: ptrdiff_t, type*, type&. So in our case it would have been enough with:

class Iterator

: public std::iterator<std::random_access_iterator_tag, double>

{ ... };

We had a discussion about deriving from std iterators which we correctly turned down but I think iterator<> should be used.

Change History (3)

comment:1 Changed 14 years ago by Markus Ringnér

Owner: changed from Jari Häkkinen to Peter

comment:2 Changed 14 years ago by Peter

I agree we should inherit from iterator.

The description is fine for utility::vector::iterator (and its const version). These iterator are very much like iterators we are used to - they model pointers to elements in the vector. The lookup iterators are a bit different. They do not have an address they are pointing to because the double they are pointing to does not exist. Therefore when calling operator* we cannot return value_type&, which in their case would be const double&. We cant return a reference to a temporary variable. The solution was to instead return const double. The difference for the caller should be very small. The problem is, though, some semantics. Either operator* cannot return reference (but e.g. return_type) or we have to set reference to be const double.

Initially I introduced the tag return_type that is used to define what type operator* returns. I did this to avoid interfering with standard definitions. This was probably to be too careful, and I think we can use reference to set what is returned from operator* because that is how std uses it and I don't think that would violate anything else (?).

comment:3 Changed 13 years ago by Peter

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.