brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 9caa90f Raw
73 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// <array>10 11// reference operator[](size_type); // constexpr in C++1712// Libc++ marks it as noexcept13 14#include <array>15#include <cassert>16 17#include "test_macros.h"18 19TEST_CONSTEXPR_CXX17 bool tests() {20  {21    typedef double T;22    typedef std::array<T, 3> C;23    C c = {1, 2, 3.5};24    LIBCPP_ASSERT_NOEXCEPT(c[0]);25    ASSERT_SAME_TYPE(C::reference, decltype(c[0]));26    C::reference r1 = c[0];27    assert(r1 == 1);28    r1 = 5.5;29    assert(c.front() == 5.5);30 31    C::reference r2 = c[2];32    assert(r2 == 3.5);33    r2 = 7.5;34    assert(c.back() == 7.5);35  }36 37  // Test operator[] "works" on zero sized arrays38  {39    {40      typedef double T;41      typedef std::array<T, 0> C;42      C c = {};43      LIBCPP_ASSERT_NOEXCEPT(c[0]);44      ASSERT_SAME_TYPE(C::reference, decltype(c[0]));45      if (c.size() > (0)) { // always false46        C::reference r = c[0];47        (void)r;48      }49    }50    {51      typedef double T;52      typedef std::array<const T, 0> C;53      C c = {};54      LIBCPP_ASSERT_NOEXCEPT(c[0]);55      ASSERT_SAME_TYPE(C::reference, decltype(c[0]));56      if (c.size() > (0)) { // always false57        C::reference r = c[0];58        (void)r;59      }60    }61  }62 63  return true;64}65 66int main(int, char**) {67  tests();68#if TEST_STD_VER >= 1769  static_assert(tests(), "");70#endif71  return 0;72}73