1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <algorithm> 10 11 // template<InputIterator Iter, Callable<auto, Iter::reference> Function> 12 // requires CopyConstructible<Function> 13 // constexpr Function // constexpr after C++17 14 // for_each(Iter first, Iter last, Function f); 15 16 #include <algorithm> 17 #include <cassert> 18 19 #include "test_macros.h" 20 #include "test_iterators.h" 21 22 #if TEST_STD_VER > 17 23 TEST_CONSTEXPR bool test_constexpr() { 24 int ia[] = {1, 3, 6, 7}; 25 int expected[] = {3, 5, 8, 9}; 26 27 std::for_each(std::begin(ia), std::end(ia), [](int &a) { a += 2; }); 28 return std::equal(std::begin(ia), std::end(ia), std::begin(expected)) 29 ; 30 } 31 #endif 32 33 struct for_each_test 34 { 35 for_each_test(int c) : count(c) {} 36 int count; 37 void operator()(int& i) {++i; ++count;} 38 }; 39 40 int main(int, char**) 41 { 42 int ia[] = {0, 1, 2, 3, 4, 5}; 43 const unsigned s = sizeof(ia)/sizeof(ia[0]); 44 for_each_test f = std::for_each(cpp17_input_iterator<int*>(ia), 45 cpp17_input_iterator<int*>(ia+s), 46 for_each_test(0)); 47 assert(f.count == s); 48 for (unsigned i = 0; i < s; ++i) 49 assert(ia[i] == static_cast<int>(i+1)); 50 51 #if TEST_STD_VER > 17 52 static_assert(test_constexpr()); 53 #endif 54 55 return 0; 56 } 57