brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · c62c9fc Raw
81 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// REQUIRES: std-at-least-c++2610 11// <optional>12 13// constexpr iterator optional::end() noexcept;14// constexpr const_iterator optional::end() const noexcept;15 16#include <cassert>17#include <iterator>18#include <optional>19#include <ranges>20#include <type_traits>21#include <utility>22 23template <typename T>24constexpr bool test() {25  std::optional<T> disengaged{std::nullopt};26 27  { // end() is marked noexcept28    static_assert(noexcept(disengaged.end()));29    static_assert(noexcept(std::as_const(disengaged).end()));30  }31 32  { // end() == begin() and end() == end() if the optional is disengaged33    auto it  = disengaged.end();34    auto it2 = std::as_const(disengaged).end();35 36    assert(it == disengaged.begin());37    assert(disengaged.begin() == it);38    assert(it == disengaged.end());39 40    assert(it2 == std::as_const(disengaged).begin());41    assert(std::as_const(disengaged).begin() == it2);42    assert(it2 == std::as_const(disengaged).end());43  }44 45  std::remove_reference_t<T> t = std::remove_reference_t<T>{};46  std::optional<T> engaged{t};47 48  { // end() != begin() if the optional is engaged49    auto it  = engaged.end();50    auto it2 = std::as_const(engaged).end();51 52    assert(it != engaged.begin());53    assert(engaged.begin() != it);54 55    assert(it2 != std::as_const(engaged).begin());56    assert(std::as_const(engaged).begin() != it2);57  }58 59  return true;60}61 62constexpr bool tests() {63  assert(test<int>());64  assert(test<char>());65  assert(test<const int>());66  assert(test<const char>());67  assert(test<int&>());68  assert(test<char&>());69  assert(test<const int&>());70  assert(test<const char&>());71 72  return true;73}74 75int main(int, char**) {76  assert(tests());77  static_assert(tests());78 79  return 0;80}81