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 transform_view(View, F); // explicit since C++23 |
12 | |
13 | #include <cassert> |
14 | #include <ranges> |
15 | |
16 | #include "test_convertible.h" |
17 | #include "test_macros.h" |
18 | |
19 | struct Range : std::ranges::view_base { |
20 | constexpr explicit Range(int* b, int* e) : begin_(b), end_(e) {} |
21 | constexpr int* begin() const { return begin_; } |
22 | constexpr int* end() const { return end_; } |
23 | |
24 | private: |
25 | int* begin_; |
26 | int* end_; |
27 | }; |
28 | |
29 | struct F { |
30 | constexpr int operator()(int i) const { return i + 100; } |
31 | }; |
32 | |
33 | // SFINAE tests. |
34 | |
35 | #if TEST_STD_VER >= 23 |
36 | |
37 | static_assert(!test_convertible<std::ranges::transform_view<Range, F>, Range, F>(), |
38 | "This constructor must be explicit" ); |
39 | |
40 | #else |
41 | |
42 | static_assert( test_convertible<std::ranges::transform_view<Range, F>, Range, F>(), |
43 | "This constructor must not be explicit" ); |
44 | |
45 | #endif // TEST_STD_VER >= 23 |
46 | |
47 | constexpr bool test() { |
48 | int buff[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
49 | |
50 | { |
51 | Range range(buff, buff + 8); |
52 | F f; |
53 | std::ranges::transform_view<Range, F> view(range, f); |
54 | assert(view[0] == 101); |
55 | assert(view[1] == 102); |
56 | // ... |
57 | assert(view[7] == 108); |
58 | } |
59 | |
60 | return true; |
61 | } |
62 | |
63 | int main(int, char**) { |
64 | test(); |
65 | static_assert(test()); |
66 | |
67 | return 0; |
68 | } |
69 | |