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