Boost logo

Boost :

Subject: Re: [boost] [iterators] Proof-of-concept for a sentinel iterator adapter
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2009-05-13 17:00:13

Steven Watanabe wrote:
> template<class T, class Sentinel>
> class sentinel_iterator {
> public:
> sentinel_iterator(T* ptr, Sentinel s = Sentinel());
> sentinel_iterator& operator++() {
> ++data.first();
> if(data.second()(*data.first())) data.first() = 0;
> }
> //...
> private:
> boost::compressed_pair<T*, Sentinel> data;
> };
Hmm, if Sentinel can't be optimized away at compile time then we don't want to copy it around when we pass the iterator by value. Similarly, we don't want to cast it by value every time it is checked as in my earlier code. Instead we want to enable the static const optimization Beman had in mind. To do this I changed the casting operator of the struct I provide as the sentinel parameter to cast to a const reference instead of T by value. The sentinel_iterator itself stays the same as it was in my previous posting.

static const int sc_val = -1;
struct s_val {
        operator const int& () const { return sc_val; }

In this way the user can provide a static const reference as the template parameter for the sentinel value if they feel the need for such optimization. If it is an iterator over objects that are expensive to copy or default construct you would not want to cast by value or default construct the sentinel value in the iterator.


Boost list run by bdawes at, gregod at, cpdaniel at, john at