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 | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 26 | int main() |
| 27 | { |
| 28 | { |
| 29 | typedef double T; |
| 30 | typedef std::array<T, 3> C; |
| 31 | C c = {1, 2, 3.5}; |
| 32 | C::reference r1 = c.at(0); |
| 33 | assert(r1 == 1); |
| 34 | r1 = 5.5; |
| 35 | assert(c.front() == 5.5); |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 36 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 37 | C::reference r2 = c.at(2); |
| 38 | assert(r2 == 3.5); |
| 39 | r2 = 7.5; |
| 40 | assert(c.back() == 7.5); |
| 41 | |
| 42 | try { (void) c.at(3); } |
| 43 | catch (const std::out_of_range &) {} |
| 44 | } |
| 45 | { |
| 46 | typedef double T; |
| 47 | typedef std::array<T, 3> C; |
| 48 | const C c = {1, 2, 3.5}; |
| 49 | C::const_reference r1 = c.at(0); |
| 50 | assert(r1 == 1); |
| 51 | |
| 52 | C::const_reference r2 = c.at(2); |
| 53 | assert(r2 == 3.5); |
| 54 | |
| 55 | try { (void) c.at(3); } |
| 56 | catch (const std::out_of_range &) {} |
| 57 | } |
| 58 | |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 59 | #if TEST_STD_VER > 11 |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 60 | { |
| 61 | typedef double T; |
| 62 | typedef std::array<T, 3> C; |
| 63 | constexpr C c = {1, 2, 3.5}; |
| 64 | |
| 65 | constexpr T t1 = c.at(0); |
| 66 | static_assert (t1 == 1, ""); |
| 67 | |
| 68 | constexpr T t2 = c.at(2); |
| 69 | static_assert (t2 == 3.5, ""); |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | } |