xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.transform/ctor.default.pass.cpp (revision b8cb1dc9ea87faa8e8e9ab7a31710a8c0bb8b084)
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 {
DefaultConstructibleViewDefaultConstructibleView22   constexpr DefaultConstructibleView() : begin_(buff), end_(buff + 3) { }
beginDefaultConstructibleView23   constexpr int const* begin() const { return begin_; }
endDefaultConstructibleView24   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_;
DefaultConstructibleFunctionDefaultConstructibleFunction32   constexpr DefaultConstructibleFunction() : state_(100) { }
operator ()DefaultConstructibleFunction33   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 
test()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 
main(int,char **)71 int main(int, char**) {
72   test();
73   static_assert(test());
74 
75   return 0;
76 }
77