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