brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 5755274 Raw
76 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#include <cassert>14#include <optional>15#include <type_traits>16#include <utility>17 18template <typename RefType, std::remove_reference_t<RefType> _Val>19constexpr bool test() {20  std::remove_reference_t<RefType> item{_Val};21  std::optional<RefType> opt{item};22 23  {24    assert(*opt == item);25    assert(&(*opt) == &item);26  }27  {28    assert(*std::as_const(opt) == item);29    assert(&(*std::as_const(opt)) == &item);30  }31 32  return true;33}34 35template <typename T>36constexpr T foo(T val) {37  return val;38}39 40template <typename T, T _Val>41constexpr bool fn_ref_test() {42  std::optional<T (&)(T)> opt{foo<T>};43  assert(opt.has_value());44  assert((*opt)(_Val) == _Val);45 46  return true;47}48 49template <typename T, T _Val>50constexpr bool array_ref_test() {51  T arr[5]{};52  std::optional<T(&)[5]> opt{arr};53 54  assert(opt.has_value());55  (*opt)[0] = _Val;56  assert((*opt)[0] == _Val);57  assert(arr[0] == _Val);58 59  return true;60}61 62constexpr bool tests() {63  assert((test<int&, 1>()));64  assert((test<double&, 1.0>()));65  assert((fn_ref_test<int, 1>()));66  assert((array_ref_test<int, 1>()));67  assert((fn_ref_test<double, 1.0>()));68  assert((array_ref_test<double, 1.0>()));69  return true;70}71 72int main(int, char**) {73  static_assert(tests());74  tests();75}76