find_if.pass.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <algorithm>
  10. // template<InputIterator Iter, Predicate<auto, Iter::value_type> Pred>
  11. // requires CopyConstructible<Pred>
  12. // constexpr Iter // constexpr after C++17
  13. // find_if(Iter first, Iter last, Pred pred);
  14. #include <algorithm>
  15. #include <functional>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "test_iterators.h"
  19. struct eq {
  20. TEST_CONSTEXPR eq (int val) : v(val) {}
  21. TEST_CONSTEXPR bool operator () (int v2) const { return v == v2; }
  22. int v;
  23. };
  24. #if TEST_STD_VER > 17
  25. TEST_CONSTEXPR bool test_constexpr() {
  26. int ia[] = {1, 3, 5, 2, 4, 6};
  27. int ib[] = {1, 2, 3, 7, 5, 6};
  28. eq c(4);
  29. return (std::find_if(std::begin(ia), std::end(ia), c) == ia+4)
  30. && (std::find_if(std::begin(ib), std::end(ib), c) == ib+6)
  31. ;
  32. }
  33. #endif
  34. int main()
  35. {
  36. int ia[] = {0, 1, 2, 3, 4, 5};
  37. const unsigned s = sizeof(ia)/sizeof(ia[0]);
  38. input_iterator<const int*> r = std::find_if(input_iterator<const int*>(ia),
  39. input_iterator<const int*>(ia+s),
  40. eq(3));
  41. assert(*r == 3);
  42. r = std::find_if(input_iterator<const int*>(ia),
  43. input_iterator<const int*>(ia+s),
  44. eq(10));
  45. assert(r == input_iterator<const int*>(ia+s));
  46. #if TEST_STD_VER > 17
  47. static_assert(test_constexpr());
  48. #endif
  49. }