[type_erasure] any and shared_ptr

Hi Steven, I need to use the associated types but don't get them to do what I need. Consider struct Pointee : public boost::enable_shared_from_this<Pointee> { void foo() { std::cout << "Pointee::foo called" << std::endl; //shared_from_this(); std::cout << "Pointee::foo end" << std::endl; } }; Pointee needs to be enable_shared_from_this for some reason, which forces me to use it in a shared_ptr. I reused your pointee / pointer structures: template<class T> struct pointee { typedef typename mpl::eval_if< boost::type_erasure::is_placeholder<T>, mpl::identity<void>, boost::pointee<T> >::type type; }; template<class T = _self> struct pointer : mpl::vector< copy_constructible<T>, dereferenceable<deduced<pointee<T> >&, T> > { typedef deduced<pointee<T> > element_type; }; I (naively) tried to define a pointee concept: typedef mpl::vector< pointer<>, same_type<pointer<>::element_type,_a >, relaxed_match, has_foo<void()> > PointeeConcept; // will not compile, shared_ptr<Pointee> has no foo... boost::shared_ptr<Pointee> p (new Pointee) ; any<PointeeConcept> ap (p); ap.foo(); So in the end I had to resort to a wrapper: struct PointeeWrap { PointeeWrap(boost::shared_ptr<Pointee>p):myPointee(p){} void foo() { std::cout << "PointeeWrap::foo called" << std::endl; myPointee->foo(); } boost::shared_ptr<Pointee> myPointee; }; BOOST_TYPE_ERASURE_MEMBER((has_foo), foo, 0); typedef any< mpl::vector< relaxed_match, copy_constructible<>, has_foo<void()> >
AnyPointee;
boost::shared_ptr<Pointee> p (new Pointee); PointeeWrap wrap(p); AnyPointee ap(wrap); ap.foo(); But this will become tedious with an increasing number of members. Is there a better way? Thanks, Christophe

AMDG On 10/16/2012 07:52 AM, Christophe Henry wrote:
Hi Steven,
I need to use the associated types but don't get them to do what I need. Consider
struct Pointee : public boost::enable_shared_from_this<Pointee> { void foo() { std::cout << "Pointee::foo called" << std::endl; //shared_from_this(); std::cout << "Pointee::foo end" << std::endl; } };
Pointee needs to be enable_shared_from_this for some reason, which forces me to use it in a shared_ptr. I reused your pointee / pointer structures:
<snip>
I (naively) tried to define a pointee concept:
typedef mpl::vector< pointer<>, same_type<pointer<>::element_type,_a >, relaxed_match, has_foo<void()> > PointeeConcept;
// will not compile, shared_ptr<Pointee> has no foo... boost::shared_ptr<Pointee> p (new Pointee) ; any<PointeeConcept> ap (p); ap.foo();
The placeholder _self maps to shared_ptr<Pointee> and the placeholder _a maps to Pointee. has_foo<void()> is actually has_foo<void(), _self> because of the default argument. This says that shared_ptr<Pointee> has a member called foo. What you really want is has_foo<void(), _a> to say that foo is a member of Pointee. In Christ, Steven Watanabe

typedef mpl::vector< pointer<>, same_type<pointer<>::element_type,_a >, relaxed_match, has_foo<void()> > PointeeConcept;
// will not compile, shared_ptr<Pointee> has no foo... boost::shared_ptr<Pointee> p (new Pointee) ; any<PointeeConcept> ap (p); ap.foo();
The placeholder _self maps to shared_ptr<Pointee> and the placeholder _a maps to Pointee. has_foo<void()> is actually has_foo<void(), _self> because of the default argument. This says that shared_ptr<Pointee> has a member called foo. What you really want is has_foo<void(), _a> to say that foo is a member of Pointee.
In Christ, Steven Watanabe
Ok, got it. I made a second mistake, I need (*ap).foo(); Thanks, Christophe
participants (2)
-
Christophe Henry
-
Steven Watanabe