Boost logo

Boost Users :

Subject: Re: [Boost-users] [type_erasure] any holding a ref to any
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2017-05-31 14:43:40


AMDG

On 05/31/2017 03:25 AM, Joaquin M López Muñoz via Boost-users wrote:
> Say I have an object a of type
>
> any<mpl::vector<Concept1,typeid_<>>,_self&>
>
> and an object b of type
>
> any<Concept2,T>
>
> where Concept1 is a subconcept of Concept2. Is it possible to make
> a refer to b rather than the object wrapped by b? I mean:
>
> any_cast<void*>(&a)==&b
> typeid_of(a)==typeid(b)
>
> A solution involving wrapping/copying b into some auxiliary entity would
> be acceptable too. This questin is obviously connected to the
> implementation of boost::any_collection, as Steven will undoubtedly have
> guessed.
>

  You can do this using the (internal, undocumented)
constructor any(detail::storage, const binding&);
which effectively lets you pass a void* and and
and vtable. With the public API, you need an
auxiliary concept like this:

#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/type_erasure/operators.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/map.hpp>
#include <iostream>

using namespace boost::type_erasure;
namespace mpl = boost::mpl;

typedef mpl::vector<copy_constructible<>, ostreamable<> > test_concept;

template<class P, class T>
struct make_reference {
  static P& apply(T& t) {
    return t;
  }
};

int main() {
  any<test_concept> x(42);
  using extra_concept =
    mpl::vector<make_reference<_self, any<test_concept> > >;
  binding<extra_concept> b1 =
    make_binding<mpl::map<mpl::pair<_self, any<test_concept> > > >();
  binding<mpl::vector<test_concept, typeid_<> > > b2 =
    make_binding<mpl::map<mpl::pair<_self, any<test_concept> > > >();
  any<mpl::vector<test_concept, typeid_<> >, _self&> ref(
    call(b1, make_reference<_self, any<test_concept> >(), x), b2);
  std::cout << ref << std::endl;
}

In Christ,
Steven Watanabe


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