xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.take.while/pred.pass.cpp (revision a2c6a1193f41e40840a7ead6c1c0540d3062c13a)
1*a2c6a119SHui Xie //===----------------------------------------------------------------------===//
2*a2c6a119SHui Xie //
3*a2c6a119SHui Xie // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*a2c6a119SHui Xie // See https://llvm.org/LICENSE.txt for license information.
5*a2c6a119SHui Xie // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*a2c6a119SHui Xie //
7*a2c6a119SHui Xie //===----------------------------------------------------------------------===//
8*a2c6a119SHui Xie 
9*a2c6a119SHui Xie // UNSUPPORTED: c++03, c++11, c++14, c++17
10*a2c6a119SHui Xie 
11*a2c6a119SHui Xie // constexpr const Pred& pred() const;
12*a2c6a119SHui Xie 
13*a2c6a119SHui Xie #include <cassert>
14*a2c6a119SHui Xie #include <ranges>
15*a2c6a119SHui Xie #include <type_traits>
16*a2c6a119SHui Xie #include <utility>
17*a2c6a119SHui Xie 
18*a2c6a119SHui Xie struct View : std::ranges::view_interface<View> {
19*a2c6a119SHui Xie   int* begin() const;
20*a2c6a119SHui Xie   int* end() const;
21*a2c6a119SHui Xie };
22*a2c6a119SHui Xie 
23*a2c6a119SHui Xie struct Pred {
24*a2c6a119SHui Xie   int i;
25*a2c6a119SHui Xie   bool operator()(int) const;
26*a2c6a119SHui Xie };
27*a2c6a119SHui Xie 
test()28*a2c6a119SHui Xie constexpr bool test() {
29*a2c6a119SHui Xie   // &
30*a2c6a119SHui Xie   {
31*a2c6a119SHui Xie     std::ranges::take_while_view<View, Pred> twv{{}, Pred{5}};
32*a2c6a119SHui Xie     decltype(auto) x = twv.pred();
33*a2c6a119SHui Xie     static_assert(std::same_as<decltype(x), Pred const&>);
34*a2c6a119SHui Xie     assert(x.i == 5);
35*a2c6a119SHui Xie   }
36*a2c6a119SHui Xie 
37*a2c6a119SHui Xie   // const &
38*a2c6a119SHui Xie   {
39*a2c6a119SHui Xie     const std::ranges::take_while_view<View, Pred> twv{{}, Pred{5}};
40*a2c6a119SHui Xie     decltype(auto) x = twv.pred();
41*a2c6a119SHui Xie     static_assert(std::same_as<decltype(x), Pred const&>);
42*a2c6a119SHui Xie     assert(x.i == 5);
43*a2c6a119SHui Xie   }
44*a2c6a119SHui Xie 
45*a2c6a119SHui Xie   // &&
46*a2c6a119SHui Xie   {
47*a2c6a119SHui Xie     std::ranges::take_while_view<View, Pred> twv{{}, Pred{5}};
48*a2c6a119SHui Xie     decltype(auto) x = std::move(twv).pred();
49*a2c6a119SHui Xie     static_assert(std::same_as<decltype(x), Pred const&>);
50*a2c6a119SHui Xie     assert(x.i == 5);
51*a2c6a119SHui Xie   }
52*a2c6a119SHui Xie 
53*a2c6a119SHui Xie   // const &&
54*a2c6a119SHui Xie   {
55*a2c6a119SHui Xie     const std::ranges::take_while_view<View, Pred> twv{{}, Pred{5}};
56*a2c6a119SHui Xie     decltype(auto) x = std::move(twv).pred();
57*a2c6a119SHui Xie     static_assert(std::same_as<decltype(x), Pred const&>);
58*a2c6a119SHui Xie     assert(x.i == 5);
59*a2c6a119SHui Xie   }
60*a2c6a119SHui Xie 
61*a2c6a119SHui Xie   return true;
62*a2c6a119SHui Xie }
63*a2c6a119SHui Xie 
main(int,char **)64*a2c6a119SHui Xie int main(int, char**) {
65*a2c6a119SHui Xie   test();
66*a2c6a119SHui Xie   static_assert(test());
67*a2c6a119SHui Xie   return 0;
68*a2c6a119SHui Xie }
69