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
18template<class It> struct Common { It begin() const; It end() const; };
19template<class It> struct NonCommon { It begin() const; sentinel_wrapper<It> end() const; };
20template<class It, class Sent> struct Range { It begin() const; Sent end() const; };
21
22static_assert(!std::ranges::common_range<Common<cpp17_input_iterator<int*>>>); // not a sentinel for itself
23static_assert(!std::ranges::common_range<Common<cpp20_input_iterator<int*>>>); // not a sentinel for itself
24static_assert( std::ranges::common_range<Common<forward_iterator<int*>>>);
25static_assert( std::ranges::common_range<Common<bidirectional_iterator<int*>>>);
26static_assert( std::ranges::common_range<Common<random_access_iterator<int*>>>);
27static_assert( std::ranges::common_range<Common<contiguous_iterator<int*>>>);
28static_assert( std::ranges::common_range<Common<int*>>);
29
30static_assert(!std::ranges::common_range<NonCommon<cpp17_input_iterator<int*>>>);
31static_assert(!std::ranges::common_range<NonCommon<cpp20_input_iterator<int*>>>);
32static_assert(!std::ranges::common_range<NonCommon<forward_iterator<int*>>>);
33static_assert(!std::ranges::common_range<NonCommon<bidirectional_iterator<int*>>>);
34static_assert(!std::ranges::common_range<NonCommon<random_access_iterator<int*>>>);
35static_assert(!std::ranges::common_range<NonCommon<contiguous_iterator<int*>>>);
36static_assert(!std::ranges::common_range<NonCommon<int*>>);
37
38// Test when begin() and end() only differ by their constness.
39static_assert(!std::ranges::common_range<Range<int*, int const*>>);
40
41// Simple test with a sized_sentinel.
42static_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.
45static_assert( std::ranges::common_range<Common<forward_iterator<int*>> const>);
46static_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.
49struct Range1 {
50 int* begin();
51 int const* begin() const;
52 int const* end() const;
53};
54static_assert(!std::ranges::common_range<Range1>);
55static_assert( std::ranges::common_range<Range1 const>);
56
57// Test with a range that's a common_range only when not const-qualified.
58struct Range2 {
59 int* begin() const;
60 int* end();
61 int const* end() const;
62};
63static_assert( std::ranges::common_range<Range2>);
64static_assert(!std::ranges::common_range<Range2 const>);
65
66// Test ADL-proofing.
67struct Incomplete;
68template<class T> struct Holder { T t; };
69
70static_assert(!std::ranges::common_range<Holder<Incomplete>*>);
71static_assert(!std::ranges::common_range<Holder<Incomplete>*&>);
72static_assert(!std::ranges::common_range<Holder<Incomplete>*&&>);
73static_assert(!std::ranges::common_range<Holder<Incomplete>* const>);
74static_assert(!std::ranges::common_range<Holder<Incomplete>* const&>);
75static_assert(!std::ranges::common_range<Holder<Incomplete>* const&&>);
76
77static_assert( std::ranges::common_range<Holder<Incomplete>*[10]>);
78static_assert( std::ranges::common_range<Holder<Incomplete>*(&)[10]>);
79static_assert( std::ranges::common_range<Holder<Incomplete>*(&&)[10]>);
80static_assert( std::ranges::common_range<Holder<Incomplete>* const[10]>);
81static_assert( std::ranges::common_range<Holder<Incomplete>* const(&)[10]>);
82static_assert( std::ranges::common_range<Holder<Incomplete>* const(&&)[10]>);
83

source code of libcxx/test/std/ranges/range.req/range.refinements/common_range.compile.pass.cpp