0

Is it possible to get next node/element directly from node/element? Like so:

struct Data{
   boost::intrusive::list_member_hook<> node;

   Data* get_next(){
       node.get_next() ???
   }
}
tower120
  • 5,007
  • 6
  • 40
  • 88

1 Answers1

1

Many boost intrusive containers have a static member function s_iterator_to to get an iterator directly from a value.

Thus you can get an iterator then use the iterator interface:

struct Data{
   boost::intrusive::list_member_hook<> node;

   inline Data* get_next();
}

namespace bis = boost::intrusive;

using List = bis::list<Data,
        bis::member_hook<Data, bis::list_member_hook<>, &Data::node>
      >;

Data* Data::get_next() {
    return &*++List::s_iterator_to(*this);
}

Live Demo

llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • What about terminal nodes? `get_next()` should return `null_ptr` if there is no next node. – tower120 Dec 17 '18 at 14:49
  • 1
    @tower120 It's impossible. Intrusive list uses a head node as sentinel. An entry node doesn't have enough info in itself to determine whether it's a terminal. nodes are not null-terminated. – llllllllll Dec 17 '18 at 16:50