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 | // transform_view() requires std::default_initializable<V> && |
12 | // std::default_initializable<F> = default; |
13 | |
14 | #include <ranges> |
15 | |
16 | #include <cassert> |
17 | #include <type_traits> |
18 | |
19 | constexpr int buff[] = {1, 2, 3}; |
20 | |
21 | struct DefaultConstructibleView : std::ranges::view_base { |
22 | constexpr DefaultConstructibleView() : begin_(buff), end_(buff + 3) { } |
23 | constexpr int const* begin() const { return begin_; } |
24 | constexpr int const* end() const { return end_; } |
25 | private: |
26 | int const* begin_; |
27 | int const* end_; |
28 | }; |
29 | |
30 | struct DefaultConstructibleFunction { |
31 | int state_; |
32 | constexpr DefaultConstructibleFunction() : state_(100) { } |
33 | constexpr int operator()(int i) const { return i + state_; } |
34 | }; |
35 | |
36 | struct NoDefaultCtrView : std::ranges::view_base { |
37 | NoDefaultCtrView() = delete; |
38 | int* begin() const; |
39 | int* end() const; |
40 | }; |
41 | |
42 | struct NoDefaultFunction { |
43 | NoDefaultFunction() = delete; |
44 | constexpr int operator()(int i) const; |
45 | }; |
46 | |
47 | constexpr bool test() { |
48 | { |
49 | std::ranges::transform_view<DefaultConstructibleView, DefaultConstructibleFunction> view; |
50 | assert(view.size() == 3); |
51 | assert(view[0] == 101); |
52 | assert(view[1] == 102); |
53 | assert(view[2] == 103); |
54 | } |
55 | |
56 | { |
57 | std::ranges::transform_view<DefaultConstructibleView, DefaultConstructibleFunction> view = {}; |
58 | assert(view.size() == 3); |
59 | assert(view[0] == 101); |
60 | assert(view[1] == 102); |
61 | assert(view[2] == 103); |
62 | } |
63 | |
64 | static_assert(!std::is_default_constructible_v<std::ranges::transform_view<NoDefaultCtrView, DefaultConstructibleFunction>>); |
65 | static_assert(!std::is_default_constructible_v<std::ranges::transform_view<DefaultConstructibleView, NoDefaultFunction>>); |
66 | static_assert(!std::is_default_constructible_v<std::ranges::transform_view<NoDefaultCtrView, NoDefaultFunction>>); |
67 | |
68 | return true; |
69 | } |
70 | |
71 | int main(int, char**) { |
72 | test(); |
73 | static_assert(test()); |
74 | |
75 | return 0; |
76 | } |
77 | |