mirror of
https://github.com/boostorg/iterator.git
synced 2025-05-12 14:01:37 +00:00
Support lambda expressions in function_input_iterator
This commit is contained in:
parent
711a0232f8
commit
c09c8ca2b2
@ -17,6 +17,7 @@
|
|||||||
#include <boost/iterator/iterator_facade.hpp>
|
#include <boost/iterator/iterator_facade.hpp>
|
||||||
#include <boost/none.hpp>
|
#include <boost/none.hpp>
|
||||||
#include <boost/optional/optional.hpp>
|
#include <boost/optional/optional.hpp>
|
||||||
|
#include <boost/utility/result_of.hpp>
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
@ -28,9 +29,9 @@ namespace iterators {
|
|||||||
class function_input_iterator
|
class function_input_iterator
|
||||||
: public iterator_facade<
|
: public iterator_facade<
|
||||||
function_input_iterator<Function, Input>,
|
function_input_iterator<Function, Input>,
|
||||||
typename Function::result_type,
|
BOOST_DEDUCED_TYPENAME result_of<Function ()>::type,
|
||||||
single_pass_traversal_tag,
|
single_pass_traversal_tag,
|
||||||
typename Function::result_type const &
|
BOOST_DEDUCED_TYPENAME result_of<Function ()>::type const &
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -46,7 +47,7 @@ namespace iterators {
|
|||||||
++state;
|
++state;
|
||||||
}
|
}
|
||||||
|
|
||||||
typename Function::result_type const &
|
BOOST_DEDUCED_TYPENAME result_of<Function ()>::type const &
|
||||||
dereference() const {
|
dereference() const {
|
||||||
return (value ? value : value = (*f)()).get();
|
return (value ? value : value = (*f)()).get();
|
||||||
}
|
}
|
||||||
@ -58,7 +59,7 @@ namespace iterators {
|
|||||||
private:
|
private:
|
||||||
Function * f;
|
Function * f;
|
||||||
Input state;
|
Input state;
|
||||||
mutable optional<typename Function::result_type> value;
|
mutable optional<BOOST_DEDUCED_TYPENAME result_of<Function ()>::type> value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class Function, class Input>
|
template <class Function, class Input>
|
||||||
|
@ -96,6 +96,23 @@ int main(int argc, char * argv[])
|
|||||||
assert(generated[i] == 42 + i);
|
assert(generated[i] == 42 + i);
|
||||||
cout << "function iterator test with stateful function object successful." << endl;
|
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<int>().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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user