Opened 16 years ago
Closed 16 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 16 years ago by
Owner: | changed from Jari Häkkinen to Peter |
---|
comment:2 Changed 16 years ago by
comment:3 Changed 16 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
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 beconst 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 usereference
to set what is returned from operator* because that is how std uses it and I don't think that would violate anything else (?).