#include #include #include #include #include #include using namespace std::placeholders; using boost::coroutines::coroutine; template coroutine outputs_of(F f) { return coroutine{[f](typename coroutine::caller_type& caller) { f(boost::make_function_output_iterator([&caller](const T& t){ caller(t); })); }}; } template void fun1( InputIterator begin, InputIterator end, OutputIterator out ) { // This one multiplies each element by two. while (begin != end) { *out = *begin * 2; ++out; ++begin; } } template void fun2( InputIterator begin, InputIterator end, OutputIterator out ) { // This one adds one to each element. while (begin != end) { *out = *begin + 1; ++out; ++begin; } } // Wrap fun1 into function object so it's usable with bind() struct fun1_ { template void operator()(InputIterator begin, InputIterator end, OutputIterator out) const { fun1(begin, end, out); } }; int main() { std::vector source = {1, 2, 3, 4}; auto coro = outputs_of(std::bind(fun1_(), source.begin(), source.end(), _1)); fun2(begin(coro), end(coro), std::ostream_iterator(std::cout, "\n")); }