find_if.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/fusion/container/vector/vector.hpp>
  8. #include <boost/fusion/adapted/mpl.hpp>
  9. #include <boost/fusion/sequence/io/out.hpp>
  10. #include <boost/fusion/algorithm/query/find_if.hpp>
  11. #include <boost/mpl/vector.hpp>
  12. #include <boost/mpl/vector_c.hpp>
  13. #include <boost/mpl/less.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. struct X
  16. {
  17. operator int() const
  18. {
  19. return 12345;
  20. }
  21. };
  22. int
  23. main()
  24. {
  25. using namespace boost::fusion;
  26. {
  27. using boost::is_same;
  28. using boost::mpl::_;
  29. typedef vector<int, char, int, double> vector_type;
  30. vector_type v(12345, 'x', 678910, 3.36);
  31. std::cout << *find_if<is_same<_, char> >(v) << std::endl;
  32. BOOST_TEST((*find_if<is_same<_, char> >(v) == 'x'));
  33. std::cout << *find_if<is_same<_, int> >(v) << std::endl;
  34. BOOST_TEST((*find_if<is_same<_, int> >(v) == 12345));
  35. std::cout << *find_if<is_same<_, double> >(v) << std::endl;
  36. BOOST_TEST((*find_if<is_same<_, double> >(v) == 3.36));
  37. }
  38. {
  39. using boost::mpl::vector;
  40. using boost::is_same;
  41. using boost::mpl::_;
  42. typedef vector<int, char, X, double> mpl_vec;
  43. BOOST_TEST((*find_if<is_same<_, X> >(mpl_vec()) == 12345));
  44. }
  45. {
  46. using boost::mpl::vector_c;
  47. using boost::mpl::less;
  48. using boost::mpl::int_;
  49. using boost::is_same;
  50. using boost::mpl::_;
  51. typedef vector_c<int, 1, 2, 3, 4> mpl_vec;
  52. BOOST_TEST((*find_if<less<_, int_<3> > >(mpl_vec()) == 1));
  53. }
  54. return boost::report_errors();
  55. }