xref: /llvm-project/libcxx/test/std/ranges/range.req/range.refinements/common_range.compile.pass.cpp (revision d2baefae6846765eef6a6dd69d4fdf1082ce29ad)
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-has-no-incomplete-ranges
11 
12 // template<class R>
13 // concept common_range;
14 
15 #include <ranges>
16 
17 #include "test_iterators.h"
18 
19 template<class It>             struct Common { It begin() const; It end() const; };
20 template<class It>             struct NonCommon { It begin() const; sentinel_wrapper<It> end() const; };
21 template<class It, class Sent> struct Range { It begin() const; Sent end() const; };
22 
23 static_assert(!std::ranges::common_range<Common<cpp17_input_iterator<int*>>>); // not a sentinel for itself
24 static_assert(!std::ranges::common_range<Common<cpp20_input_iterator<int*>>>); // not a sentinel for itself
25 static_assert( std::ranges::common_range<Common<forward_iterator<int*>>>);
26 static_assert( std::ranges::common_range<Common<bidirectional_iterator<int*>>>);
27 static_assert( std::ranges::common_range<Common<random_access_iterator<int*>>>);
28 static_assert( std::ranges::common_range<Common<contiguous_iterator<int*>>>);
29 static_assert( std::ranges::common_range<Common<int*>>);
30 
31 static_assert(!std::ranges::common_range<NonCommon<cpp17_input_iterator<int*>>>);
32 static_assert(!std::ranges::common_range<NonCommon<cpp20_input_iterator<int*>>>);
33 static_assert(!std::ranges::common_range<NonCommon<forward_iterator<int*>>>);
34 static_assert(!std::ranges::common_range<NonCommon<bidirectional_iterator<int*>>>);
35 static_assert(!std::ranges::common_range<NonCommon<random_access_iterator<int*>>>);
36 static_assert(!std::ranges::common_range<NonCommon<contiguous_iterator<int*>>>);
37 static_assert(!std::ranges::common_range<NonCommon<int*>>);
38 
39 // Test when begin() and end() only differ by their constness.
40 static_assert(!std::ranges::common_range<Range<int*, int const*>>);
41 
42 // Simple test with a sized_sentinel.
43 static_assert(!std::ranges::common_range<Range<int*, sized_sentinel<int*>>>);
44 
45 // Make sure cv-qualification doesn't impact the concept when begin() and end() have matching qualifiers.
46 static_assert( std::ranges::common_range<Common<forward_iterator<int*>> const>);
47 static_assert(!std::ranges::common_range<NonCommon<forward_iterator<int*>> const>);
48 
49 // Test with a range that's a common_range only when const-qualified.
50 struct Range1 {
51   int* begin();
52   int const* begin() const;
53   int const* end() const;
54 };
55 static_assert(!std::ranges::common_range<Range1>);
56 static_assert( std::ranges::common_range<Range1 const>);
57 
58 // Test with a range that's a common_range only when not const-qualified.
59 struct Range2 {
60   int* begin() const;
61   int* end();
62   int const* end() const;
63 };
64 static_assert( std::ranges::common_range<Range2>);
65 static_assert(!std::ranges::common_range<Range2 const>);
66 
67 // Test ADL-proofing.
68 struct Incomplete;
69 template<class T> struct Holder { T t; };
70 
71 static_assert(!std::ranges::common_range<Holder<Incomplete>*>);
72 static_assert(!std::ranges::common_range<Holder<Incomplete>*&>);
73 static_assert(!std::ranges::common_range<Holder<Incomplete>*&&>);
74 static_assert(!std::ranges::common_range<Holder<Incomplete>* const>);
75 static_assert(!std::ranges::common_range<Holder<Incomplete>* const&>);
76 static_assert(!std::ranges::common_range<Holder<Incomplete>* const&&>);
77 
78 static_assert( std::ranges::common_range<Holder<Incomplete>*[10]>);
79 static_assert( std::ranges::common_range<Holder<Incomplete>*(&)[10]>);
80 static_assert( std::ranges::common_range<Holder<Incomplete>*(&&)[10]>);
81 static_assert( std::ranges::common_range<Holder<Incomplete>* const[10]>);
82 static_assert( std::ranges::common_range<Holder<Incomplete>* const(&)[10]>);
83 static_assert( std::ranges::common_range<Holder<Incomplete>* const(&&)[10]>);
84