| 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 auto begin() requires (!simple-view<V>) |
| 12 |
// constexpr auto begin() const requires range<const V> |
| 13 |
|
| 14 |
#include <cassert> |
| 15 |
#include <concepts> |
| 16 |
#include <ranges> |
| 17 |
#include <tuple> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
#include "types.h" |
| 21 |
|
| 22 |
template <class T> |
| 23 |
concept HasConstBegin = requires(const T ct) { ct.begin(); }; |
| 24 |
|
| 25 |
template <class T> |
| 26 |
concept HasBegin = requires(T t) { t.begin(); }; |
| 27 |
|
| 28 |
template <class T> |
| 29 |
concept HasConstAndNonConstBegin = |
| 30 |
HasConstBegin<T> && |
| 31 |
// because const begin and non-const begin returns different types (iterator<true>, iterator<false>) |
| 32 |
requires(T t, const T ct) { requires !std::same_as<decltype(t.begin()), decltype(ct.begin())>; }; |
| 33 |
|
| 34 |
template <class T> |
| 35 |
concept HasOnlyNonConstBegin = HasBegin<T> && !HasConstBegin<T>; |
| 36 |
|
| 37 |
template <class T> |
| 38 |
concept HasOnlyConstBegin = HasConstBegin<T> && !HasConstAndNonConstBegin<T>; |
| 39 |
|
| 40 |
struct NoConstBeginView : TupleBufferView { |
| 41 |
using TupleBufferView::TupleBufferView; |
| 42 |
constexpr std::tuple<int>* begin() { return buffer_; } |
| 43 |
constexpr std::tuple<int>* end() { return buffer_ + size_; } |
| 44 |
}; |
| 45 |
|
| 46 |
// simple-view<V> |
| 47 |
static_assert(HasOnlyConstBegin<std::ranges::elements_view<SimpleCommon, 0>>); |
| 48 |
|
| 49 |
// !simple-view<V> && range<const V> |
| 50 |
static_assert(HasConstAndNonConstBegin<std::ranges::elements_view<NonSimpleCommon, 0>>); |
| 51 |
|
| 52 |
// !range<const V> |
| 53 |
static_assert(HasOnlyNonConstBegin<std::ranges::elements_view<NoConstBeginView, 0>>); |
| 54 |
|
| 55 |
constexpr bool test() { |
| 56 |
std::tuple<int> buffer[] = {{1}, {2}}; |
| 57 |
{ |
| 58 |
// underlying iterator should be pointing to the first element |
| 59 |
auto ev = std::views::elements<0>(buffer); |
| 60 |
auto iter = ev.begin(); |
| 61 |
assert(&(*iter) == &std::get<0>(buffer[0])); |
| 62 |
} |
| 63 |
|
| 64 |
{ |
| 65 |
// underlying range models simple-view |
| 66 |
auto v = std::views::elements<0>(SimpleCommon{buffer}); |
| 67 |
static_assert(std::is_same_v<decltype(v.begin()), decltype(std::as_const(v).begin())>); |
| 68 |
assert(v.begin() == std::as_const(v).begin()); |
| 69 |
auto&& r = *std::as_const(v).begin(); |
| 70 |
assert(&r == &std::get<0>(buffer[0])); |
| 71 |
} |
| 72 |
|
| 73 |
{ |
| 74 |
// underlying const R is not a range |
| 75 |
auto v = std::views::elements<0>(NoConstBeginView{buffer}); |
| 76 |
auto&& r = *v.begin(); |
| 77 |
assert(&r == &std::get<0>(buffer[0])); |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
int main(int, char**) { |
| 84 |
test(); |
| 85 |
static_assert(test()); |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |