From c09c8ca2b27de4f493c1569f6a5f968263c96642 Mon Sep 17 00:00:00 2001 From: nekko1119 Date: Mon, 28 Dec 2015 02:45:49 +0900 Subject: [PATCH] Support lambda expressions in function_input_iterator --- .../boost/iterator/function_input_iterator.hpp | 9 +++++---- test/function_input_iterator_test.cpp | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/include/boost/iterator/function_input_iterator.hpp b/include/boost/iterator/function_input_iterator.hpp index 8a793df..1ad91b3 100644 --- a/include/boost/iterator/function_input_iterator.hpp +++ b/include/boost/iterator/function_input_iterator.hpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace boost { @@ -28,9 +29,9 @@ namespace iterators { class function_input_iterator : public iterator_facade< function_input_iterator, - typename Function::result_type, + BOOST_DEDUCED_TYPENAME result_of::type, single_pass_traversal_tag, - typename Function::result_type const & + BOOST_DEDUCED_TYPENAME result_of::type const & > { public: @@ -46,7 +47,7 @@ namespace iterators { ++state; } - typename Function::result_type const & + BOOST_DEDUCED_TYPENAME result_of::type const & dereference() const { return (value ? value : value = (*f)()).get(); } @@ -58,7 +59,7 @@ namespace iterators { private: Function * f; Input state; - mutable optional value; + mutable optional::type> value; }; template diff --git a/test/function_input_iterator_test.cpp b/test/function_input_iterator_test.cpp index b08caa6..e57ca2a 100644 --- a/test/function_input_iterator_test.cpp +++ b/test/function_input_iterator_test.cpp @@ -96,6 +96,23 @@ int main(int argc, char * argv[]) assert(generated[i] == 42 + i); cout << "function iterator test with stateful function object successful." << endl; +#if !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) + // test the iterator with lambda expressions + int num = 42; + auto lambda_generator = [&num] { return num++; }; + vector().swap(generated); + copy( + boost::make_function_input_iterator(lambda_generator, 0), + boost::make_function_input_iterator(lambda_generator, 10), + back_inserter(generated) + ); + + assert(generated.size() == 10); + for(std::size_t i = 0; i != 10; ++i) + assert(generated[i] == 42 + i); + cout << "function iterator test with lambda expressions successful." << endl; +#endif // BOOST_NO_CXX11_LAMBDAS + return 0; }