privately inherit enable_shared_from_this?

I have some code that uses enable_shared_from_this. I was publicly deriving, i.e. (and this works) class Request : public boost::enable_shared_from_this<Request> { }; I wanted to change my code around so that I wrapped the shared_from_this function inside the class, so that I could check at runtime whether certain conditions hold, i.e. class Request : private boost::enable_shared_from_this<Request> { public: boost::shared_ptr<Request> get_ptr () { if (conditions_are_good) return shared_from this (); else throw std::logic_error ("Bad get"); }; }; This won't compile (visual studio 2008). 1>Compiling... 1>Request.cpp 1>c:\boost\boost_1_40_0\boost\smart_ptr\enable_shared_from_this.hpp(50) : error C2243: 'type cast' : conversion from 'Request *' to 'boost::enable_shared_from_this<T> *const ' exists, but is inaccessible 1> with 1> [ 1> T=Request 1> ] 1> c:\boost\boost_1_40_0\boost\smart_ptr\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)' 1> with 1> [ 1> T=Request 1> ] 1> c:\src\cpp\trunk\request_lib\request.h(48) : see reference to class template instantiation 'boost::enable_shared_from_this<T>' being compiled 1> with 1> [ 1> T=Request 1> ] Can I get this to work?

2009/12/11 Eric J. Holtman
class Request : private boost::enable_shared_from_this<Request> { public: boost::shared_ptr<Request> get_ptr () { if (conditions_are_good) return shared_from this (); else throw std::logic_error ("Bad get"); }; };
Try declaring boost::enable_shared_from_this<Request> a friend of Request. Roman Perepelitsa.

Eric J. Holtman wrote:
I have some code that uses enable_shared_from_this.
I was publicly deriving, i.e. (and this works)
class Request : public boost::enable_shared_from_this<Request> { };
I wanted to change my code around so that I wrapped the shared_from_this function inside the class, so that I could check at runtime whether certain conditions hold, i.e.
class Request : private boost::enable_shared_from_this<Request> { public: boost::shared_ptr<Request> get_ptr () { if (conditions_are_good) return shared_from this (); else throw std::logic_error ("Bad get"); }; };
This won't compile (visual studio 2008).
One way to make it work is to add friend class boost::enable_shared_from_this<Request>; friend class boost::shared_ptr<Request>; to Request.

Peter Dimov wrote:
One way to make it work is to add
friend class boost::enable_shared_from_this<Request>; friend class boost::shared_ptr<Request>;
to Request. _______________________________________________
Hmmm.... this "works", but Request is actually the root of a very large tree, and all my nodes get broken in the same way now. i.e. class Forecast_Snapshot_Request : public Request { boost::shared_ptr<Request> build () { return shared_ptr<Request> (new Forecast_Snapshot_Request); }; }; fails with 1>c:\boost\boost_1_40_0\boost\smart_ptr\shared_ptr.hpp(189) : error C2243: 'type cast' : conversion from 'Forecast_Snapshot_Request *' to 'const boost::enable_shared_from_this<T> *' exists, but is inaccessible I tried add more friend decls, but no dice. Sigh. Maybe I'll just add documention to call my wrapper, not shared_from_this ().
participants (3)
-
Eric J. Holtman
-
Peter Dimov
-
Roman Perepelitsa