xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.take.while/base.pass.cpp (revision a2c6a1193f41e40840a7ead6c1c0540d3062c13a)
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 V base() const & requires copy_constructible<V> { return base_; }
12 // constexpr V base() && { return std::move(base_); }
13 
14 #include <cassert>
15 #include <ranges>
16 #include <type_traits>
17 #include <utility>
18 
19 #include "MoveOnly.h"
20 #include "types.h"
21 
22 struct View : std::ranges::view_interface<View> {
23   int i;
24   int* begin() const;
25   int* end() const;
26 };
27 
28 struct MoveOnlyView : View {
29   MoveOnly mo;
30 };
31 
32 template <class T>
33 concept HasBase = requires(T&& t) { std::forward<T>(t).base(); };
34 
35 struct Pred {
operator ()Pred36   constexpr bool operator()(int i) const { return i > 5; }
37 };
38 
39 static_assert(HasBase<std::ranges::take_while_view<View, Pred> const&>);
40 static_assert(HasBase<std::ranges::take_while_view<View, Pred>&&>);
41 
42 static_assert(!HasBase<std::ranges::take_while_view<MoveOnlyView, Pred> const&>);
43 static_assert(HasBase<std::ranges::take_while_view<MoveOnlyView, Pred>&&>);
44 
test()45 constexpr bool test() {
46   // const &
47   {
48     const std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};
49     std::same_as<View> decltype(auto) v = twv.base();
50     assert(v.i == 5);
51   }
52 
53   // &
54   {
55     std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};
56     std::same_as<View> decltype(auto) v = twv.base();
57     assert(v.i == 5);
58   }
59 
60   // &&
61   {
62     std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};
63     std::same_as<View> decltype(auto) v = std::move(twv).base();
64     assert(v.i == 5);
65   }
66 
67   // const &&
68   {
69     const std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};
70     std::same_as<View> decltype(auto) v = std::move(twv).base();
71     assert(v.i == 5);
72   }
73 
74   // move only
75   {
76     std::ranges::take_while_view<MoveOnlyView, Pred> twv{MoveOnlyView{{}, 5}, {}};
77     std::same_as<MoveOnlyView> decltype(auto) v = std::move(twv).base();
78     assert(v.mo.get() == 5);
79   }
80   return true;
81 }
82 
main(int,char **)83 int main(int, char**) {
84   test();
85   static_assert(test());
86   return 0;
87 }
88