Boost logo

Boost Users :

From: John (john_at_[hidden])
Date: 2008-04-06 14:06:22


I'm trying to get boost range and iterator to work. I've got so many questions,
I don't know where to start. So, rather than post a specific compiler error,
let's go back to square one. How about a concrete example of fitting a UDT with
Boost.Range?

Say I have a class with a non-trivial iterator type (not a pointer!) I'd like
to modify the class so that Boost.Range is happy. At some point, I feel as
though I just end up arbitrarily copying snippets from the docs in the hope that
it will all work. Is there something I'm not "getting" about boost.range?

The code below is an attempt to create a working skeleton example. Obviously,
the author lacks whatever magic spell book is required to put it all together --
"it" being a user-defined type, plus Boost.Range. Seems like something as
simple as a modeling a range should be easier than breathing, so I'm hoping it
actually is, and that I'm just missing something totally obvious to everyone else.

So, is the following skeleton correct? Where does it go awry? Can we perhaps
clean it up, make it perfect, and introduce it (or something like it) to the
Boost.Range docs?

// An arbitrary container-esque class.
class Foo {
public:

     class iterator : public boost::iterator_facade <
                          iterator,
                          Foo::value_type,
                          boost::random_access_traversal_tag,
                          Foo::value_type,
                          ptrdiff_t // Difference type?
>
     {
     public:
         iterator (); // no-arg ctor required by boost?
     private:
         iterator (internal_stuff); // "real" ctor
         friend class boost::iterator_core_access;
         friend class Foo;

         // Required by iterator_facade?
         void increment();
         void decrement();
         void advance (ptrdiff_t);
         ptrdiff_t distance_to (const iterator&) const;
         bool equal (const iterator&) const;
         Foo::value_type dereference() const;
     };

     // Required by Boost.Range?
     iterator end() const;
     iterator begin() const;

     // Blatant shot in the dark here...
     typedef boost::sub_range< Foo > range_type;

     // Walking blindfolded, with earplugs, and wearing mittens.
     range_type
     produce_a_range_somehow () const
     {
           iterator a = this->begin();
           iterator b = this->begin();
           some_algorithm_here (a, b, this->end());
           return range_type (a, b);
     }

     // Now it gets bad.
     void
     use_a_range_somehow (range_type r)
     {
         // Starting to feel as though Kansas is far, far away.
         // Wondering where the past 4 hours went.
         // Wondering why I don't just use a custom std::pair-like range.
         // Am I cursed or blessed by the commitment to Boost?
     }
};


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net