brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 824e8f8 Raw
89 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 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 22template <class T>23concept HasConstBegin = requires(const T ct) { ct.begin(); };24 25template <class T>26concept HasBegin = requires(T t) { t.begin(); };27 28template <class T>29concept 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 34template <class T>35concept HasOnlyNonConstBegin = HasBegin<T> && !HasConstBegin<T>;36 37template <class T>38concept HasOnlyConstBegin = HasConstBegin<T> && !HasConstAndNonConstBegin<T>;39 40struct 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>47static_assert(HasOnlyConstBegin<std::ranges::elements_view<SimpleCommon, 0>>);48 49// !simple-view<V> && range<const V>50static_assert(HasConstAndNonConstBegin<std::ranges::elements_view<NonSimpleCommon, 0>>);51 52// !range<const V>53static_assert(HasOnlyNonConstBegin<std::ranges::elements_view<NoConstBeginView, 0>>);54 55constexpr bool test() {56  std::tuple<int> buffer[] = {{1}, {2}};57  {58    // underlying iterator should be pointing to the first element59    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-view66    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 range75    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 83int main(int, char**) {84  test();85  static_assert(test());86 87  return 0;88}89