Boost logo

Boost :

From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2008-08-14 19:01:00


The changes made to enable_shared_from_this since 1.35 cause classes to
be larger than they were before.

I created a small test that I ran against 1.35 and the current trunk.
These are the results for VC9 SP1 release mode.

BOOST_VERSION = 103500
sizeof(SharedFromThisEnabled) = 8
sizeof(MultipleInheritanceDerived) = 12
sizeof(ChainedBaseDerived) = 12

BOOST_VERSION = 103600
sizeof(SharedFromThisEnabled) = 12
sizeof(MultipleInheritanceDerived) = 16
sizeof(ChainedBaseDerived) = 12

Did the changes made offer functionality worth the extra space?

The MultipleInheritanceDerived pattern is recommended in most of the
Boost.Asio examples. If the changes made truly offer some value perhaps
a base class chaining version of enabled_shared_from_this should be
provided.

Thanks,

Michael Marcin


#include <iostream>
#include <boost/enable_shared_from_this.hpp>
#include <boost/version.hpp>

class SharedFromThisEnabled
   : public boost::enable_shared_from_this<SharedFromThisEnabled>
{
public:
};

class AbstractBase
{
public:
   virtual void Foo() = 0;
};

class MultipleInheritanceDerived
   : public AbstractBase
    , public boost::enable_shared_from_this<MultipleInheritanceDerived>
{
public:
   virtual void Foo() {}
};

#if (BOOST_VERSION==103500)
   template< class T, class Base >
   class enable_shared_from_this_chained_base
      : public Base
   {
   public:

      typedef T _internal_element_type; // for bcc 5.5.1
      mutable boost::weak_ptr<_internal_element_type> _internal_weak_this;
   };

#elif (BOOST_VERSION==103600)
   template< class T, class Base >
   class enable_shared_from_this_chained_base
      : public Base
   {
   protected:

      // virtual destructor because we need a vtable for dynamic_cast from base to derived to work
      virtual ~enable_shared_from_this_chained_base()
      {
      }

   private:

      mutable boost::detail::weak_count _weak_count;
      mutable boost::detail::shared_count _shared_count;
   };
#else
#error "Update enable_shared_from_this_chained_base implementation and version number"
#endif

class ChainedBaseDerived
   : public enable_shared_from_this_chained_base<ChainedBaseDerived,AbstractBase>
{
public:
   virtual void Foo() {}
};

int main()
{
   std::cout
      << "BOOST_VERSION = " << BOOST_VERSION << std::endl
      << "sizeof(SharedFromThisEnabled) = " << sizeof(SharedFromThisEnabled) << std::endl
      << "sizeof(MultipleInheritanceDerived) = " << sizeof(MultipleInheritanceDerived) << std::endl
      << "sizeof(ChainedBaseDerived) = " << sizeof(ChainedBaseDerived) << std::endl
      ;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk