Boost logo

Boost :

From: Kühl, Dietmar (Dietmar.Kuehl_at_[hidden])
Date: 1998-12-08 04:50:05


David Abrahams (abrahams_at_[hidden]) wrote:
> Beman Dawes (beman_at_[hidden]) wrote:
> >I
> >don't have any problem requiring the specific name (say "use_count")
> >for the embedded counter.
> I do: It could cause unneccessary collisions and unintended effects!
> It's so easy to use base class; why not do it that way?
I don't like inheritance for purposes like this: You don't gain anything
but practically a certain member. It is actually nothing more than a
data member and I prefer data members to be exactly this. However,
if basically the method scheme is used I have proposed, ie. a
template class called 'shared_ptr' taking a class for the reference
counted type and a class describing the counting method, it would
be easy to add whatever methods are convenient. This may include
an embedded reference counted pointer like you proposed as well
as any other scheme.

My major worry with the base class approach is that it does not fit
well with existing hierarchies. Of course, a point can be made that
this is bad design but then it is not always possible to influence the
design. For example, this is the case if libraries are used or if you
are in a large project.

... and, of course, there is another issue: Embedded reference counts
are for efficiency purposes, amoung other efficency considerations
for space efficency. If it is known somehow that the number of
simultaneous references is relatively small (eg. < 100) it can be
reasonable to use a spare 'unsigned char' or some bits of a bitfield for
a reference count (well, this bitfield is not covered by my previous
proposal). ... and, yes, I have this situation! I use reference
counted pointers to maintain edge lists in large sparse graphs (eg.
[parts of] the European railroad system). For each edge I need
some Boolean flags and I have some spare bytes which are left
empty for alignment purposes. I could use one of them for reference
counting since the reference count is fairly small (I think it is at most
4 during some graph manipulations).

> P.S. If you don't like the 2nd optional argument from embedded_ptr for
> types that want a different reference count type, we could ask the user
> to specialize boost::reference_counted_traits<> for their
> intrusively-counted types, supplying a count_type.
If you use a base class, why not have the type be a typedef in the
base class?

  namespace boost {

    template <class Count = std::size_t>
    struct reference_counted {
      typedef Count use_count_type;
      use_count_type use_count;

    template <class T>
    class embedded_ptr
      typedef typename T::use_count_type use_count_type;

I think there was some problem with the visibility of typedefs scoped
in classes and I'm not sure whether this would apply here.Whether it
does apply may depend on whether 'T' is a template class.

Free Web-based e-mail groups --

Boost list run by bdawes at, gregod at, cpdaniel at, john at