Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Jan 2014 09:48:33 -0700
From:      Ian Lepore <ian@FreeBSD.org>
To:        Poul-Henning Kamp <phk@phk.freebsd.dk>
Cc:        Hans Petter Selasky <hps@bitfrost.no>, "freebsd-hackers@freebsd.org" <freebsd-hackers@FreeBSD.org>
Subject:   Re: Make "sys/queue.h" usable with C++
Message-ID:  <1389890913.1230.64.camel@revolution.hippie.lan>
In-Reply-To: <16417.1389881910@critter.freebsd.dk>
References:  <52D7D302.3090403@bitfrost.no> <1679.1389879981@critter.freebsd.dk> <52D7E674.4010501@bitfrost.no> <16417.1389881910@critter.freebsd.dk>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 2014-01-16 at 14:18 +0000, Poul-Henning Kamp wrote:
> In message <52D7E674.4010501@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





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1389890913.1230.64.camel>