Can I use the type of "this" as a template argument (inside a macro)?
I currently have this:
#define THIS(T) (boost::static_pointer_cast<T>(shared_from_this()))
The macro is used in a method like this:
void Derived::run() {
do_something_with_a_shared_ptr(THIS(Derived));
}
That's all well and good, but I'd like to eliminate (Derived)
and have:
void Derived::run() {
do_something_with_a_shared_ptr(THIS);
}
Is this possible?
Alternatively, is there a better way to have convenient access to a shared_ptr
to this
, in a class derived (indirectly)开发者_开发技巧 from boost::enable_shared_from_this
? This question seems to indicate the answer to this is no.
The class hierarchy looks like this:
class Base: public boost::enable_shared_from_this<Base> {
...
}
class Derived: public Base {
...
void run();
...
}
void do_something_with_a_shared_ptr(boost::shared_ptr<Derived>);
Not exactly an answer to your question, but have you considered using a member function instead of a macro? I usually do something like:
boost::shared_ptr< Derived > shared_this()
{
return boost::static_pointer_cast<Derived>(shared_from_this());
}
boost::shared_ptr< Derived const > shared_this() const
{
return boost::static_pointer_cast<Derived const>(shared_from_this());
}
精彩评论