111 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 end() requires (!simple-view<V>)12// { return sentinel<false>(ranges::end(base_), addressof(*pred_)); }13// constexpr auto end() const14// requires range<const V> &&15// indirect_unary_predicate<const Pred, iterator_t<const V>>16// { return sentinel<true>(ranges::end(base_), addressof(*pred_)); }17 18#include <cassert>19#include <ranges>20#include <type_traits>21#include <utility>22 23#include "types.h"24 25// Test Constraints26template <class T>27concept HasConstEnd = requires(const T& ct) { ct.end(); };28 29template <class T>30concept HasEnd = requires(T& t) { t.end(); };31 32template <class T>33concept HasConstAndNonConstEnd =34 HasConstEnd<T> && requires(T& t, const T& ct) { requires !std::same_as<decltype(t.end()), decltype(ct.end())>; };35 36template <class T>37concept HasOnlyNonConstEnd = HasEnd<T> && !HasConstEnd<T>;38 39template <class T>40concept HasOnlyConstEnd = HasConstEnd<T> && !HasConstAndNonConstEnd<T>;41 42struct Pred {43 constexpr bool operator()(int i) const { return i < 5; }44};45 46static_assert(HasOnlyConstEnd<std::ranges::take_while_view<SimpleView, Pred>>);47 48static_assert(HasOnlyNonConstEnd<std::ranges::take_while_view<ConstNotRange, Pred>>);49 50static_assert(HasConstAndNonConstEnd<std::ranges::take_while_view<NonSimple, Pred>>);51 52struct NotPredForConst {53 constexpr bool operator()(int& i) const { return i > 5; }54};55static_assert(HasOnlyNonConstEnd<std::ranges::take_while_view<NonSimple, NotPredForConst>>);56 57constexpr bool test() {58 // simple-view59 {60 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};61 SimpleView v{buffer};62 std::ranges::take_while_view twv(v, Pred{});63 decltype(auto) it1 = twv.end();64 assert(it1 == buffer + 4);65 decltype(auto) it2 = std::as_const(twv).end();66 assert(it2 == buffer + 4);67 68 static_assert(std::same_as<decltype(it1), decltype(it2)>);69 }70 71 // const not range72 {73 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};74 ConstNotRange v{buffer};75 std::ranges::take_while_view twv(v, Pred{});76 decltype(auto) it1 = twv.end();77 assert(it1 == buffer + 4);78 }79 80 // NonSimple81 {82 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};83 NonSimple v{buffer};84 std::ranges::take_while_view twv(v, Pred{});85 decltype(auto) it1 = twv.end();86 assert(it1 == buffer + 4);87 decltype(auto) it2 = std::as_const(twv).end();88 assert(it2 == buffer + 4);89 90 static_assert(!std::same_as<decltype(it1), decltype(it2)>);91 }92 93 // NotPredForConst94 // LWG 3450: The const overloads of `take_while_view::begin/end` are underconstrained95 {96 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};97 NonSimple v{buffer};98 std::ranges::take_while_view twv(v, NotPredForConst{});99 decltype(auto) it1 = twv.end();100 assert(it1 == buffer);101 }102 103 return true;104}105 106int main(int, char**) {107 test();108 static_assert(test());109 return 0;110}111