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 InIter, OutputIterator<auto, InIter::reference> OutIter,
12// Predicate<auto, InIter::value_type> Pred>
13// requires CopyConstructible<Pred>
14// constexpr OutIter // constexpr after C++17
15// copy_if(InIter first, InIter last, OutIter result, Pred pred);
16
17#include <algorithm>
18#include <cassert>
19
20#include "test_macros.h"
21#include "test_iterators.h"
22#include "type_algorithms.h"
23
24struct Pred {
25 TEST_CONSTEXPR_CXX14 bool operator()(int i) { return i % 3 == 0; }
26};
27
28template <class InIter>
29struct TestOutIters {
30 template <class OutIter>
31 TEST_CONSTEXPR_CXX20 void operator()() {
32 const unsigned N = 1000;
33 int ia[N] = {};
34 for (unsigned i = 0; i < N; ++i)
35 ia[i] = i;
36 int ib[N] = {0};
37
38 OutIter r = std::copy_if(InIter(ia), InIter(ia + N), OutIter(ib), Pred());
39 assert(base(r) == ib + N / 3 + 1);
40 for (unsigned i = 0; i < N / 3 + 1; ++i)
41 assert(ib[i] % 3 == 0);
42 }
43};
44
45struct TestInIters {
46 template <class InIter>
47 TEST_CONSTEXPR_CXX20 void operator()() {
48 types::for_each(
49 types::concatenate_t<types::cpp17_input_iterator_list<int*>, types::type_list<cpp17_output_iterator<int*> > >(),
50 TestOutIters<InIter>());
51 }
52};
53
54TEST_CONSTEXPR_CXX20 bool test() {
55 types::for_each(types::cpp17_input_iterator_list<const int*>(), TestInIters());
56 return true;
57}
58
59int main(int, char**) {
60 test();
61
62#if TEST_STD_VER > 17
63 static_assert(test());
64#endif
65
66 return 0;
67}
68

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_if.pass.cpp