xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.common.view/base.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 // constexpr V base() const& requires copy_constructible<V>;
12 // constexpr V base() &&;
13 
14 #include <ranges>
15 
16 #include <cassert>
17 #include <utility>
18 
19 #include "test_macros.h"
20 #include "types.h"
21 
hasLValueQualifiedBase(auto && view)22 constexpr bool hasLValueQualifiedBase(auto&& view) {
23   return requires { view.base(); };
24 }
25 
test()26 constexpr bool test() {
27   int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
28 
29   {
30     CopyableView view{buf, buf + 8};
31     std::ranges::common_view<CopyableView> common(view);
32     assert(common.base().begin_ == buf);
33     assert(std::move(common).base().begin_ == buf);
34 
35     ASSERT_SAME_TYPE(decltype(common.base()), CopyableView);
36     ASSERT_SAME_TYPE(decltype(std::move(common).base()), CopyableView);
37     static_assert(hasLValueQualifiedBase(common));
38   }
39 
40   {
41     MoveOnlyView view{buf, buf + 8};
42     std::ranges::common_view<MoveOnlyView> common(std::move(view));
43     assert(std::move(common).base().begin_ == buf);
44 
45     ASSERT_SAME_TYPE(decltype(std::move(common).base()), MoveOnlyView);
46     static_assert(!hasLValueQualifiedBase(common));
47   }
48 
49   {
50     CopyableView view{buf, buf + 8};
51     const std::ranges::common_view<CopyableView> common(view);
52     assert(common.base().begin_ == buf);
53     assert(std::move(common).base().begin_ == buf);
54 
55     ASSERT_SAME_TYPE(decltype(common.base()), CopyableView);
56     ASSERT_SAME_TYPE(decltype(std::move(common).base()), CopyableView);
57     static_assert(hasLValueQualifiedBase(common));
58   }
59 
60   return true;
61 }
62 
main(int,char **)63 int main(int, char**) {
64   test();
65   static_assert(test());
66 
67   return 0;
68 }
69