xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.take.while/ctor.view.pass.cpp (revision 40aaa272f145e633b29d5e70a4590cc425801f7e)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // constexpr take_while_view(V base, Pred pred); // explicit since C++23
12 
13 #include <cassert>
14 #include <ranges>
15 #include <type_traits>
16 #include <utility>
17 
18 #include "MoveOnly.h"
19 #include "test_convertible.h"
20 #include "test_macros.h"
21 
22 struct View : std::ranges::view_base {
23   MoveOnly mo;
24   int* begin() const;
25   int* end() const;
26 };
27 
28 struct Pred {
29   bool copied      = false;
30   bool moved       = false;
31   constexpr Pred() = default;
PredPred32   constexpr Pred(Pred&&) : moved(true) {}
PredPred33   constexpr Pred(const Pred&) : copied(true) {}
34   bool operator()(int) const;
35 };
36 
37 // SFINAE tests.
38 
39 #if TEST_STD_VER >= 23
40 
41 static_assert(!test_convertible<std::ranges::take_while_view<View, Pred>, View, Pred>(),
42               "This constructor must be explicit");
43 
44 #else
45 
46 static_assert(test_convertible<std::ranges::take_while_view<View, Pred>, View, Pred>(),
47               "This constructor must not be explicit");
48 
49 #endif // TEST_STD_VER >= 23
50 
test()51 constexpr bool test() {
52   {
53     std::ranges::take_while_view<View, Pred> twv{View{{}, MoveOnly{5}}, Pred{}};
54     assert(twv.pred().moved);
55     assert(!twv.pred().copied);
56     assert(std::move(twv).base().mo.get() == 5);
57   }
58   return true;
59 }
60 
main(int,char **)61 int main(int, char**) {
62   test();
63   static_assert(test());
64 
65   return 0;
66 }
67