any.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2005 Eric Niebler
  4. Copyright (c) Dan Marsden
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/fusion/container/vector/vector.hpp>
  10. #include <boost/fusion/adapted/mpl.hpp>
  11. #include <boost/fusion/algorithm/query/any.hpp>
  12. #include <boost/lambda/lambda.hpp>
  13. #include <boost/mpl/vector_c.hpp>
  14. namespace
  15. {
  16. struct search_for
  17. {
  18. explicit search_for(int in_search)
  19. : search(in_search)
  20. {}
  21. template<typename T>
  22. bool operator()(T const& v) const
  23. {
  24. return v == search;
  25. }
  26. int search;
  27. };
  28. }
  29. int
  30. main()
  31. {
  32. {
  33. boost::fusion::vector<int, short, double> t(1, 2, 3.3);
  34. BOOST_TEST(boost::fusion::any(t, boost::lambda::_1 == 2));
  35. }
  36. {
  37. boost::fusion::vector<int, short, double> t(1, 2, 3.3);
  38. BOOST_TEST(!boost::fusion::any(t, boost::lambda::_1 == 3));
  39. }
  40. {
  41. typedef boost::mpl::vector_c<int, 1, 2, 3> mpl_vec;
  42. // We cannot use lambda here as mpl vec iterators return
  43. // rvalues, and lambda needs lvalues.
  44. BOOST_TEST(boost::fusion::any(mpl_vec(), search_for(2)));
  45. BOOST_TEST(!boost::fusion::any(mpl_vec(), search_for(4)));
  46. }
  47. return boost::report_errors();
  48. }