/*****************/ /* Wrapper class */ /*****************/ #include #include #include template class tr1_functor { public: // tr1 result_of support template struct result; template struct result { typedef decltype(boost::declval()(boost::declval()...)) type; }; // ctors tr1_functor() {} tr1_functor(Func const& f) : f_(f) {} // assignment tr1_functor& operator=(tr1_functor const& other) { if (f_) { f_ = boost::none; } if (other.f_) { f_ = boost::in_place(other.f_.get()); } return *this; } // invoke // precondition: f_ is initialized template typename result::type operator()(Args&&... args) { return (*f_)(static_cast(args)...); } // precondition: f_ is initialized template typename result::type operator()(Args&&... args) const { return (*f_)(static_cast(args)...); } private: // data boost::optional f_; }; template tr1_functor make_tr1_functor(Func const& f) { return tr1_functor(f); } /***************/ /* Sample code */ /***************/ #include #include #include #include int main (int argc, char* argv[]) { using boost::adaptors::transformed; BOOST_FOREACH( int i , boost::irange(0,5) | transformed( make_tr1_functor([](int i){ return i * 100; }) ) ) { std::cout << i << " "; } // Output: "0 100 200 300 400" return 0; }