#include #include #include #include #include #include using std::vector; using namespace boost::type_erasure; namespace mpl = boost::mpl; //basic class declarations class Test; class Collection { public: class iterator { public: explicit iterator(int i) : index(i) {}; iterator& operator++(); Test operator* () const; bool operator== (const iterator& other) const; int index; }; Collection(int i = 1) :value(i) { }; friend iterator begin(Collection& c); friend iterator end(Collection& c); int value; }; class Test { public: Test(int i = -1) : value(i) {} #if 0 typedef vector collection_type; #else typedef Collection collection_type; #endif collection_type collection(); int value; }; ///type erasure stuff BOOST_TYPE_ERASURE_FREE((has_begin), begin, 1) BOOST_TYPE_ERASURE_FREE((has_end), end, 1) BOOST_TYPE_ERASURE_MEMBER((has_collection), collection, 0) // Calculates the return type of T::collection() template struct get_collection_type_impl { typedef typename T::collection_type type; }; template struct get_collection_type { typedef typename mpl::eval_if, mpl::identity, get_collection_type_impl >::type type; }; typedef deduced > _collection; template struct get_iterator_type_impl { typedef typename T::iterator type; }; template struct get_iterator_type { typedef typename mpl::eval_if, mpl::identity, get_iterator_type_impl >::type type; }; typedef deduced > _iterator; typedef boost::mpl::vector< // requirements on test relaxed, copy_constructible<>, has_collection<_collection()>, // requirements on _collection copy_constructible<_collection>, has_begin<_iterator(_collection&)>, has_end<_iterator(_collection&)>, // requirements on the iterator (using _self as the value_type) copy_constructible<_iterator>, dereferenceable<_self, _iterator>, incrementable<_iterator>, equality_comparable<_iterator> > requirements; typedef any test; typedef any erased_collection; typedef any erased_iterator; ////implementation Test::collection_type Test::collection() { return collection_type(); } Collection::iterator begin(Collection& c) { return Collection::iterator(c.value); } Collection::iterator end(Collection& c) { return Collection::iterator(c.value + 5); } Collection::iterator& Collection::iterator::operator++() { index++; return *this; } bool Collection::iterator::operator==(const Collection::iterator& other) const { return index == other.index; } Test Collection::iterator::operator*() const { return Test(index); } int main(int argc, const char *argv[]) { test t; t = Test(); //cout << t.value(); auto tests = t.collection(); auto a = begin(tests); auto b = end(tests); //currently works up to here a == b; // would like this to work for (auto i : tests) { //what type would i be though? Test? test? Test& x = any_cast(i); std::cout << x.value << std::endl; } }