Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <array> |
| 11 | |
| 12 | // reference operator[] (size_type) |
| 13 | // const_reference operator[] (size_type); // constexpr in C++14 |
| 14 | // reference at (size_type) |
| 15 | // const_reference at (size_type); // constexpr in C++14 |
| 16 | |
| 17 | #include <array> |
| 18 | #include <cassert> |
| 19 | |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 20 | #include "test_macros.h" |
| 21 | |
Eric Fiselier | b4e2e7a | 2015-10-01 07:05:38 +0000 | [diff] [blame] | 22 | // std::array is explicitly allowed to be initialized with A a = { init-list };. |
| 23 | // Disable the missing braces warning for this reason. |
| 24 | #include "disable_missing_braces_warning.h" |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 25 | |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 26 | #if TEST_STD_VER > 14 |
| 27 | constexpr bool check_idx( size_t idx, double val ) |
Stephan T. Lavavej | a730ed3 | 2017-01-18 20:10:25 +0000 | [diff] [blame] | 28 | { |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 29 | std::array<double, 3> arr = {1, 2, 3.5}; |
Stephan T. Lavavej | 4159db7 | 2017-07-29 00:55:10 +0000 | [diff] [blame] | 30 | return arr[idx] == val; |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 31 | } |
| 32 | #endif |
| 33 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 34 | int main() |
| 35 | { |
| 36 | { |
| 37 | typedef double T; |
| 38 | typedef std::array<T, 3> C; |
| 39 | C c = {1, 2, 3.5}; |
| 40 | C::reference r1 = c[0]; |
| 41 | assert(r1 == 1); |
| 42 | r1 = 5.5; |
| 43 | assert(c.front() == 5.5); |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 44 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 45 | C::reference r2 = c[2]; |
| 46 | assert(r2 == 3.5); |
| 47 | r2 = 7.5; |
| 48 | assert(c.back() == 7.5); |
| 49 | } |
| 50 | { |
| 51 | typedef double T; |
| 52 | typedef std::array<T, 3> C; |
| 53 | const C c = {1, 2, 3.5}; |
| 54 | C::const_reference r1 = c[0]; |
| 55 | assert(r1 == 1); |
| 56 | C::const_reference r2 = c[2]; |
| 57 | assert(r2 == 3.5); |
| 58 | } |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 59 | { // Test operator[] "works" on zero sized arrays |
| 60 | typedef double T; |
| 61 | typedef std::array<T, 0> C; |
| 62 | C c = {}; |
| 63 | C const& cc = c; |
| 64 | static_assert((std::is_same<decltype(c[0]), T &>::value), ""); |
| 65 | static_assert((std::is_same<decltype(cc[0]), const T &>::value), ""); |
| 66 | if (c.size() > (0)) { // always false |
| 67 | C::reference r1 = c[0]; |
| 68 | C::const_reference r2 = cc[0]; |
| 69 | ((void)r1); |
| 70 | ((void)r2); |
| 71 | } |
| 72 | } |
| 73 | { // Test operator[] "works" on zero sized arrays |
| 74 | typedef double T; |
| 75 | typedef std::array<const T, 0> C; |
| 76 | C c = {{}}; |
| 77 | C const& cc = c; |
| 78 | static_assert((std::is_same<decltype(c[0]), const T &>::value), ""); |
| 79 | static_assert((std::is_same<decltype(cc[0]), const T &>::value), ""); |
| 80 | if (c.size() > (0)) { // always false |
| 81 | C::reference r1 = c[0]; |
| 82 | C::const_reference r2 = cc[0]; |
| 83 | ((void)r1); |
| 84 | ((void)r2); |
| 85 | } |
| 86 | } |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 87 | #if TEST_STD_VER > 11 |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 88 | { |
| 89 | typedef double T; |
| 90 | typedef std::array<T, 3> C; |
| 91 | constexpr C c = {1, 2, 3.5}; |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 92 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 93 | constexpr T t1 = c[0]; |
| 94 | static_assert (t1 == 1, ""); |
| 95 | |
| 96 | constexpr T t2 = c[2]; |
| 97 | static_assert (t2 == 3.5, ""); |
| 98 | } |
| 99 | #endif |
| 100 | |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 101 | #if TEST_STD_VER > 14 |
| 102 | { |
| 103 | static_assert (check_idx(0, 1), ""); |
| 104 | static_assert (check_idx(1, 2), ""); |
| 105 | static_assert (check_idx(2, 3.5), ""); |
| 106 | } |
| 107 | #endif |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 108 | } |