brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 716acbf Raw
63 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// <ranges>12 13//  template<class T> struct tuple_size;14//  template<size_t I, class T> struct tuple_element;15 16#include <ranges>17// Note: make sure to not include `<utility>` (or any other header including `<utility>`) because it also makes some18// tuple specializations available, thus obscuring whether the `<ranges>` includes work correctly.19 20using Iterator = int*;21 22class SizedSentinel {23public:24    constexpr bool operator==(int*) const;25    friend constexpr std::ptrdiff_t operator-(const SizedSentinel&, int*);26    friend constexpr std::ptrdiff_t operator-(int*, const SizedSentinel&);27};28 29static_assert(std::sized_sentinel_for<SizedSentinel, Iterator>);30using SizedRange = std::ranges::subrange<Iterator, SizedSentinel>;31 32using UnsizedSentinel = std::unreachable_sentinel_t;33static_assert(!std::sized_sentinel_for<UnsizedSentinel, Iterator>);34using UnsizedRange = std::ranges::subrange<Iterator, UnsizedSentinel>;35 36// Because the sentinel is unsized while the subrange is sized, an additional integer member will be used to store the37// size -- make sure it doesn't affect the value of `tuple_size`.38using ThreeElementRange = std::ranges::subrange<Iterator, UnsizedSentinel, std::ranges::subrange_kind::sized>;39static_assert(std::ranges::sized_range<ThreeElementRange>);40 41static_assert(std::tuple_size<SizedRange>::value == 2);42static_assert(std::tuple_size<UnsizedRange>::value == 2);43static_assert(std::tuple_size<ThreeElementRange>::value == 2);44 45template <int I, class Range, class Expected>46constexpr bool test_tuple_element() {47  static_assert(std::same_as<typename std::tuple_element<I, Range>::type, Expected>);48  static_assert(std::same_as<typename std::tuple_element<I, const Range>::type, Expected>);49  // Note: the Standard does not mandate a specialization of `tuple_element` for volatile, so trying a `volatile Range`50  // would fail to compile.51 52  return true;53}54 55int main(int, char**) {56  static_assert(test_tuple_element<0, SizedRange, Iterator>());57  static_assert(test_tuple_element<1, SizedRange, SizedSentinel>());58  static_assert(test_tuple_element<0, UnsizedRange, Iterator>());59  static_assert(test_tuple_element<1, UnsizedRange, UnsizedSentinel>());60 61  return 0;62}63