title: Intrusive, circular, branchless, doubly linked list - how to have the list to identify the node member fields?
body: <p>The code is posted here: <a href="https://ideone.com/ul2PiS" rel="nofollow">https://ideone.com/ul2PiS</a></p>
  
  <p>What I'm trying to do is allow the user to specify the list nodes as member fields of the classes which will be added to the lists. Currently, this is done with a macro using offsetof() which means that the member nodes have to be public. Ideally, I'd like to be able to somehow specify, as part of each linked_list declaration, as a template parameter, which member field should be used as the node. boost::intrusive <em>seems</em> to manage it, but I can't quite follow how they do it.</p>
  
  <p>It's a requirement that the list functions be branchless (no pointer comparisons, the platform has a very very slow branch mechanism) and that the list be intrusive and allow objects to be members of multiple lists simultaneously.</p>
  
  <pre><code>//////////////////////////////////////////////////////////////////////
  // linked_list.h
  
  #pragma once
  
  //////////////////////////////////////////////////////////////////////
  
  #include &lt;cstddef&gt;
  #include &lt;functional&gt;
  
  //////////////////////////////////////////////////////////////////////
  
  struct list_node
  {
      list_node *next;
      list_node *prev;
  };
  
  //////////////////////////////////////////////////////////////////////
  
  template&lt;typename T, size_t off&gt; struct linked_list
  {
      //////////////////////////////////////////////////////////////////////
  
      static list_node const *get_node(T const *o)
      {
          return reinterpret_cast&lt;list_node const *&gt;(reinterpret_cast&lt;char const *&gt;(o) + off);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      static list_node *get_node(T *o)
      {
          return reinterpret_cast&lt;list_node *&gt;(reinterpret_cast&lt;char *&gt;(o) + off);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      static T const *get_object(list_node const *node)
      {
          return reinterpret_cast&lt;T const *&gt;(reinterpret_cast&lt;char const *&gt;(node) - off);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      static T *get_object(list_node *node)
      {
          return reinterpret_cast&lt;T *&gt;(reinterpret_cast&lt;char *&gt;(node) - off);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      list_node root;
  
      //////////////////////////////////////////////////////////////////////
  
      linked_list()
      {
          root.next = &amp;root;
          root.prev = &amp;root;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void push_front(T *obj)
      {
          list_node *node = get_node(obj);
          root.next-&gt;prev = node;
          node-&gt;next = root.next;
          node-&gt;prev = &amp;root;
          root.next = node;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void push_front(T &amp;obj)
      {
          list_node *node = get_node(&amp;obj);
          root.next-&gt;prev = node;
          node-&gt;next = root.next;
          node-&gt;prev = &amp;root;
          root.next = node;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void push_back(T *obj)
      {
          list_node *node = get_node(obj);
          node-&gt;prev = root.prev;
          node-&gt;next = &amp;root;
          root.prev-&gt;next = node;
          root.prev = node;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void push_back(T &amp;obj)
      {
          list_node *node = get_node(&amp;obj);
          node-&gt;prev = root.prev;
          node-&gt;next = &amp;root;
          root.prev-&gt;next = node;
          root.prev = node;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void insert_before(T *pos, T *obj)
      {
          list_node *node = get_node(obj);
          list_node *n = get_node(pos);
          n-&gt;prev-&gt;next = node;
          node-&gt;prev = n-&gt;prev;
          n-&gt;prev = node;
          node-&gt;next = n;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void insert_before(T &amp;pos, T &amp;obj)
      {
          list_node *node = get_node(&amp;obj);
          list_node *n = get_node(&amp;pos);
          n-&gt;prev-&gt;next = node;
          node-&gt;prev = n-&gt;prev;
          n-&gt;prev = node;
          node-&gt;next = n;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void insert_after(T *pos, T *obj)
      {
          list_node *node = get_node(obj);
          list_node *n = get_node(pos);
          n-&gt;next-&gt;prev = node;
          node-&gt;next = n-&gt;next;
          n-&gt;next = node;
          node-&gt;prev = n;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void insert_after(T &amp;pos, T &amp;obj)
      {
          list_node *node = get_node(&amp;obj);
          list_node *n = get_node(&amp;pos);
          n-&gt;next-&gt;prev = node;
          node-&gt;next = n-&gt;next;
          n-&gt;next = node;
          node-&gt;prev = n;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void remove(T *obj)
      {
          list_node *node = get_node(obj);
          node-&gt;prev-&gt;next = node-&gt;next;
          node-&gt;next-&gt;prev = node-&gt;prev;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void remove(T &amp;obj)
      {
          list_node *node = get_node(&amp;obj);
          node-&gt;prev-&gt;next = node-&gt;next;
          node-&gt;next-&gt;prev = node-&gt;prev;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T *pop_back()
      {
          list_node *node = root.prev;
          node-&gt;prev-&gt;next = node-&gt;next;
          node-&gt;next-&gt;prev = node-&gt;prev;
          return get_object(node);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T *pop_front()
      {
          list_node *node = root.next;
          node-&gt;next-&gt;prev = node-&gt;prev;
          node-&gt;prev-&gt;next = node-&gt;next;
          return get_object(node);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      bool empty() const
      {
          return root.next == &amp;root;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      void clear()
      {
          root.next = root.prev = &amp;root;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T *head() const
      {
          return get_object(root.next);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T *tail() const
      {
          return get_object(root.prev);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T const *end()
      {
          return get_object(&amp;root);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T *next(T *i) const
      {
          return get_object(get_node(i)-&gt;next);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T *prev(T *i) const
      {
          return get_object(get_node(i)-&gt;prev);
      }
  
      //////////////////////////////////////////////////////////////////////
  
      bool for_each(std::function&lt;bool (T *)&gt; func)
      {
          for(T *i = head(); i != end(); i = next(i))
          {
              if(!func(i))
              {
                  return false;
              }
          }
          return true;
      }
  
      //////////////////////////////////////////////////////////////////////
  
      T *find(std::function&lt;bool (T *)&gt; func)
      {
          for(T *i = head(); i != end(); i = next(i))
          {
              if(func(i))
              {
                  return i;
              }
          }
          return nullptr;
      }
  
      //////////////////////////////////////////////////////////////////////
  
  };
  
  //////////////////////////////////////////////////////////////////////
  
  // Yuck:
  
  #define declare_linked_list(type_name, node_name) \
      linked_list&lt;type_name, offsetof(type_name, node_name)&gt;
  
  #define typedef_linked_list(type_name, node_name) \
      typedef declare_linked_list(type_name, node_name)
  </code></pre>
  
  <p>And the client code looks like:</p>
  
  <pre><code>//////////////////////////////////////////////////////////////////////
  // main.cpp
  
  #include &lt;stdio.h&gt;
  #include &lt;random&gt;
  #include "linked_list.h"
  
  struct foo
  {
      foo() : i(rand() % 10) { }
  
      int i;
  
      list_node node1;    // would like it if these could be made private
      list_node node2;    // but the nasty macros need to see inside...
      list_node node3;    // getting rid of the macros would be even better
  };
  
  // None of these 3 options are very nice:
  
  // 1. declare a list with the macro
  declare_linked_list(foo, node1) list1;
  
  // 2. or via a typedef
  typedef_linked_list(foo, node2) list2_t;
  list2_t list2;
  
  // 3. or very wordy non-macro declaration
  linked_list&lt;foo, offsetof(foo, node3)&gt; list3;
  
  int main(int, char **)
  {
      printf("Begin\n");
  
      foo foos[10];
  
      for(int i=0; i&lt;10; ++i)
      {
          list1.push_back(foos[i]);
          list2.push_back(foos[i]);
          list3.push_back(foos[i]);
      }
  
      int sum = 0;
      int n = 0;
      // but this for loop is clear and readable and has very low overhead
      for(foo *i = list1.head(); i != list1.end(); i = list1.next(i))
      {
          sum += i-&gt;i;
      }
      printf("Total: %d\n", sum);
  
      list2.remove(foos[2]);
  
      n = 0;
      sum = 0;
      for(foo *i = list2.head(); i != list2.end(); i = list2.next(i))
      {
          sum += i-&gt;i;
      }
      printf("Total2: %d\n", sum);
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>[EDIT]
  OK, with pointer to member template parameter it's much nicer:</p>
  
  <pre><code>template&lt;typename T, list_node T::* node&gt; struct linked_list
  {
      //////////////////////////////////////////////////////////////////////
  
      static list_node const *get_node(T const *o)
      {
          return reinterpret_cast&lt;list_node const *&gt;
              (reinterpret_cast&lt;char const *&gt;(o) + offsetof(T, *node));
      }
  
      //////////////////////////////////////////////////////////////////////
  
      static list_node *get_node(T *o)
      {
          return reinterpret_cast&lt;list_node *&gt;
              (reinterpret_cast&lt;char *&gt;(o) + offsetof(T, *node));
      }
  
      //////////////////////////////////////////////////////////////////////
  
      static T const *get_object(list_node const *n)
      {
          return reinterpret_cast&lt;T const *&gt;
              (reinterpret_cast&lt;char const *&gt;(n) - offsetof(T, *node));
      }
  
      //////////////////////////////////////////////////////////////////////
  
      static T *get_object(list_node *n)
      {
          return reinterpret_cast&lt;T *&gt;
              (reinterpret_cast&lt;char *&gt;(n) - offsetof(T, *node));
      }
  </code></pre>
  
  <p>so you can declare lists like:</p>
  
  <pre><code>linked_list&lt;foo, &amp;foo::node1&gt; list1;
  </code></pre>
  
  <p>so we (mostly) get rid of the ugly declaration syntax, although I still can't find a way to allow them to be private. It looks like boost::intrusive requires them to be public as well.</p>