Boost logo

Boost Users :

Subject: [Boost-users] 1.37: boost::enable_shared_from_this fails while std::tr1 succeed s
From: Daniel Lidström (daniel.lidstrom_at_[hidden])
Date: 2009-01-23 04:14:56


Hello,

this sample does not compile when using the boost version:

#if 1
#include <boost/enable_shared_from_this.hpp>
namespace tr1 = boost;
#else
#include <memory>
namespace tr1 = std::tr1;
#endif

namespace
{
   struct Base
   {
      virtual void f() = 0;
   };
   typedef tr1::shared_ptr<Base> BasePtr;

   struct Sub : public Base,
                public tr1::enable_shared_from_this<Base>
   {
      void f()
      {
         BasePtr basePtr = shared_from_this();
      }
   };
}

int main()
{
   return 0;
}

boost\enable_shared_from_this.hpp(50) : error C2446: '==' : no conversion
from 'boost::enable_shared_from_this<T> *const ' to
'`anonymous-namespace'::Base *'
        with
        [
            T=`anonymous-namespace'::Base
        ]
        Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
        boost\enable_shared_from_this.hpp(48) : while compiling class
template member function 'boost::shared_ptr<T>
boost::enable_shared_from_this<T>::shared_from_this(void)'
        with
        [
            T=`anonymous-namespace'::Base
        ]
        test.cpp(23) : see reference to class template instantiation
'boost::enable_shared_from_this<T>' being compiled
        with
        [
            T=`anonymous-namespace'::Base
        ]

The tr1 version compiles though. I am using Visual Studio 2008 with SP1
installed.
I have already found a workaround, but it is a little bit annoying:

   struct Sub : public Base,
                public tr1::enable_shared_from_this<Sub>
   {
      void f()
      {
         BasePtr basePtr =
tr1::dynamic_pointer_cast<Base>(shared_from_this());
      }
   };

I would like to avoid explicit type casts like this. Why does the boost
version fail?

Thanks in advance!

Regards,

Daniel Lidström
Stockholm, Sweden


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net