xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.take/ctor.default.pass.cpp (revision 770602cfa01346db58417fa2f6bfd49f8ed4e9fa)
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 // UNSUPPORTED: libcpp-no-concepts
11 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
12 
13 // take_view() requires default_initializable<V> = default;
14 
15 #include <ranges>
16 #include <cassert>
17 
18 int buff[8] = {1, 2, 3, 4, 5, 6, 7, 8};
19 
20 struct DefaultConstructible : std::ranges::view_base {
21   constexpr DefaultConstructible() : begin_(buff), end_(buff + 8) { }
22   constexpr int const* begin() const { return begin_; }
23   constexpr int const* end() const { return end_; }
24 private:
25   int const* begin_;
26   int const* end_;
27 };
28 
29 struct NonDefaultConstructible : std::ranges::view_base {
30   NonDefaultConstructible() = delete;
31   int* begin() const;
32   int* end() const;
33 };
34 
35 constexpr bool test() {
36   {
37     std::ranges::take_view<DefaultConstructible> tv;
38     assert(tv.begin() == buff);
39     assert(tv.size() == 0);
40   }
41 
42   // Test SFINAE-friendliness
43   {
44     static_assert( std::is_default_constructible_v<std::ranges::take_view<DefaultConstructible>>);
45     static_assert(!std::is_default_constructible_v<std::ranges::take_view<NonDefaultConstructible>>);
46   }
47 
48   return true;
49 }
50 
51 int main(int, char**) {
52   test();
53   static_assert(test());
54 
55   return 0;
56 }
57