Make "sys/queue.h" usable with C++

Ian Lepore ian at FreeBSD.org
Thu Jan 16 16:48:44 UTC 2014


On Thu, 2014-01-16 at 14:18 +0000, Poul-Henning Kamp wrote:
> In message <52D7E674.4010501 at bitfrost.no>, Hans Petter Selasky writes:
> 
> >Or make one macro for each list type?
> >
> >#ifndef TAILQ_STRUCT
> >#define TAILQ_STRUCT struct
> >#endif
> >
> >#ifndef LIST_STRUCT
> >#define LIST_STRUCT struct
> >#endif
> >
> >and so on?
> 
> lets not overthink this :-)
> 
> 

Given how nearly synonymous 'class' and 'struct' are in C++, would a
viable solution to this be dealing with it wholly within the application
space?  

That is, if you want an object to be a list entry, declare it as a
struct instead of a class, and make whatever bits of it private that you
want rather than relying on the default privateness you get with
class.  

You can also define a trivial wrapper struct around a class,

 class Foo { ... };
 struct FooEntry : public Foo {};

But of course that leaves you needing to write ctors and dtors at least
for the FooEntry.

Then of course there's the whole world of templatized solutions,
something along the lines of

  template<class T> 
  struct ListEntry {
    /* list fields */
    /* operator overload functions to convert a ListEntry<T>
     * to a reference/pointer to T
     */
  };

  class Foo : public ListEntry<foo> {
  ...
  };

If it were me, I'd probably use the template solution.  (Actually, if it
were me, I'd use STL containers, not try to wedge primitive C-language
invasive list management code into C++ objects.)

-- Ian




More information about the freebsd-hackers mailing list