#include #include #include #include #include #include #include #include #include #include #include namespace mpl = boost::mpl; using namespace boost::type_erasure; using namespace std; struct Something { Something(const char* n): myName(n){} std::string const& getName()const { return myName; } std::string myName; }; // SOLUTION1 this works template struct getName { static T apply(C const& cont) { return cont.getName(); } }; namespace boost { namespace type_erasure { template struct concept_interface< ::getName, Base, C> : Base { T getName() { return call(::getName(), *this); } }; } } // SOLUTION2 this does not //BOOST_TYPE_ERASURE_MEMBER((has_getName), getName, 0); typedef any< mpl::vector< // SOLUTION1 getName<_self,std::string const&>, // SOLUTION2 //has_getName, relaxed_match, copy_constructible<>, typeid_<> >, const _self& > AnyNameable; struct Container { typedef std::vector< AnyNameable > PublicData; Container() { Something s1("bla"); Something s2("blabla"); myData.push_back(s1); myData.push_back(s2); } PublicData getData()const { PublicData res; for (Data::const_iterator it = myData.begin(); it != myData.end() ; ++it) { res.push_back(AnyNameable(*it)); } return res; } private: typedef std::vector< Something > Data; Data myData; }; void test() { Container c; Container::PublicData data = c.getData(); for (Container::PublicData::const_iterator it = data.begin(); it != data.end() ;++it) { AnyNameable a(*it); std::cout << "data: " << a.getName() << std::endl; // auxiliary question: why does this not work if I use const _self&? //std::cout << "data: " << (*it).getName() << std::endl; } } int main() { test(); return 0; }