#include #include #include #include #include #include #include #include class T { public: char m_c; T( char c ) : m_c( c ) { } virtual T * clone() const=0; virtual ~T(){} }; class U : public T { public: U() : T('u'){} virtual T * clone() const { return new U( ); } }; class V : public T { public: V( const char c) : T(c){} virtual T * clone() const { return new V( m_c ); } }; typedef boost::shared_ptr TPtr; typedef std::vector TVec; using namespace boost::lambda; int main() { TVec tVec; const boost::function< void( T const * const) > f = bind( &TVec::push_back, &tVec, bind( constructor(), bind( &T::clone, _1))); f( &V('a') ); f( &V('b') ); f( &V('c') ); f( &U() ); std::cout << tVec.size() << std::endl; BOOST_FOREACH( TPtr t, tVec ) std::cout << t->m_c << std::endl; //The following function doesn't compile // const boost::function< void( const T & ) > g = // bind( &TVec::push_back, &tVec, // bind( constructor(), // bind( &T::clone, _1))); //Neither does this // const boost::function< void( const T & ) > g = // bind( &TVec::push_back, &tVec, // bind( constructor(), // bind( &T::clone, boost::cref(_1) ))); }