Files
clang-p2996/libcxx/test/std/ranges/range.adaptors/range.elements/begin.pass.cpp
Hui Xie 94461822c7 [libc++][ranges] implement std::views::elements_view
`subrange` is also a `tuple-like`. To avoid the add entire `subrange` dependencies to `tuple-like`, we need forward declaration of `subrange`. However, the class template constraints of `subrange` currently requires `__iterator/concepts.h`, which requires `<concepts>`. The problem is that currently `tuple-like` is used in several different places, including libc++ extension for pair constructors. we don't want to add `<concepts>` to pair and other stuff. So this change also created several small headers that `subrange`'s declaration needed inside `__iterator/concepts/`

Differential Revision: https://reviews.llvm.org/D136268
2023-01-15 17:36:10 +00:00

89 lines
2.6 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// constexpr auto begin() requires (!simple-view<V>)
// constexpr auto begin() const requires range<const V>
#include <cassert>
#include <concepts>
#include <ranges>
#include <tuple>
#include <utility>
#include "types.h"
template <class T>
concept HasConstBegin = requires(const T ct) { ct.begin(); };
template <class T>
concept HasBegin = requires(T t) { t.begin(); };
template <class T>
concept HasConstAndNonConstBegin =
HasConstBegin<T> &&
// because const begin and non-const begin returns different types (iterator<true>, iterator<false>)
requires(T t, const T ct) { requires !std::same_as<decltype(t.begin()), decltype(ct.begin())>; };
template <class T>
concept HasOnlyNonConstBegin = HasBegin<T> && !HasConstBegin<T>;
template <class T>
concept HasOnlyConstBegin = HasConstBegin<T> && !HasConstAndNonConstBegin<T>;
struct NoConstBeginView : TupleBufferView {
using TupleBufferView::TupleBufferView;
constexpr std::tuple<int>* begin() { return buffer_; }
constexpr std::tuple<int>* end() { return buffer_ + size_; }
};
// simple-view<V>
static_assert(HasOnlyConstBegin<std::ranges::elements_view<SimpleCommon, 0>>);
// !simple-view<V> && range<const V>
static_assert(HasConstAndNonConstBegin<std::ranges::elements_view<NonSimpleCommon, 0>>);
// !range<const V>
static_assert(HasOnlyNonConstBegin<std::ranges::elements_view<NoConstBeginView, 0>>);
constexpr bool test() {
std::tuple<int> buffer[] = {{1}, {2}};
{
// underlying iterator should be pointing to the first element
auto ev = std::views::elements<0>(buffer);
auto iter = ev.begin();
assert(&(*iter) == &std::get<0>(buffer[0]));
}
{
// underlying range models simple-view
auto v = std::views::elements<0>(SimpleCommon{buffer});
static_assert(std::is_same_v<decltype(v.begin()), decltype(std::as_const(v).begin())>);
assert(v.begin() == std::as_const(v).begin());
auto&& r = *std::as_const(v).begin();
assert(&r == &std::get<0>(buffer[0]));
}
{
// underlying const R is not a range
auto v = std::views::elements<0>(NoConstBeginView{buffer});
auto&& r = *v.begin();
assert(&r == &std::get<0>(buffer[0]));
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}