Boost logo

Boost :

Subject: [boost] [type_erasure] any and shared_ptr
From: Christophe Henry (christophe.j.henry_at_[hidden])
Date: 2012-10-16 10:52:46

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;
    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

template<class T>
struct pointee
    typedef typename mpl::eval_if<
>::type type;
template<class T = _self>
struct pointer :
        dereferenceable<deduced<pointee<T> >&, T>
    typedef deduced<pointee<T> > element_type;

I (naively) tried to define a pointee concept:

typedef mpl::vector<
      same_type<pointer<>::element_type,_a >,
> PointeeConcept;

  // will not compile, shared_ptr<Pointee> has no foo...
  boost::shared_ptr<Pointee> p (new Pointee) ;
  any<PointeeConcept> ap (p);;

So in the end I had to resort to a wrapper:

struct PointeeWrap
  void foo()
    std::cout << "PointeeWrap::foo called" << std::endl;
  boost::shared_ptr<Pointee> myPointee;

BOOST_TYPE_ERASURE_MEMBER((has_foo), foo, 0);
typedef any<
> AnyPointee;

  boost::shared_ptr<Pointee> p (new Pointee);
  PointeeWrap wrap(p);
  AnyPointee ap(wrap);;

But this will become tedious with an increasing number of members. Is
there a better way?


Boost list run by bdawes at, gregod at, cpdaniel at, john at