blob: 5cb89dfeeb9adc1c6e33ca99d82e989fca543bde [file] [log] [blame]
Marshall Clow8bf1f082013-07-17 18:25:36 +00001//===----------------------------------------------------------------------===//
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
Asiri Rathnayakef520c142015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Marshall Clow8bf1f082013-07-17 18:25:36 +000011// <array>
12
13// reference operator[] (size_type)
14// const_reference operator[] (size_type); // constexpr in C++14
15// reference at (size_type)
16// const_reference at (size_type); // constexpr in C++14
17
18#include <array>
19#include <cassert>
20
Eric Fiselier2decfad2015-07-18 23:56:04 +000021#include "test_macros.h"
22
Eric Fiselierb4e2e7a2015-10-01 07:05:38 +000023// std::array is explicitly allowed to be initialized with A a = { init-list };.
24// Disable the missing braces warning for this reason.
25#include "disable_missing_braces_warning.h"
Eric Fiselier2decfad2015-07-18 23:56:04 +000026
Marshall Clow8bf1f082013-07-17 18:25:36 +000027int main()
28{
29 {
30 typedef double T;
31 typedef std::array<T, 3> C;
32 C c = {1, 2, 3.5};
33 C::reference r1 = c.at(0);
34 assert(r1 == 1);
35 r1 = 5.5;
36 assert(c.front() == 5.5);
Eric Fiselier2decfad2015-07-18 23:56:04 +000037
Marshall Clow8bf1f082013-07-17 18:25:36 +000038 C::reference r2 = c.at(2);
39 assert(r2 == 3.5);
40 r2 = 7.5;
41 assert(c.back() == 7.5);
42
43 try { (void) c.at(3); }
44 catch (const std::out_of_range &) {}
45 }
46 {
47 typedef double T;
48 typedef std::array<T, 3> C;
49 const C c = {1, 2, 3.5};
50 C::const_reference r1 = c.at(0);
51 assert(r1 == 1);
52
53 C::const_reference r2 = c.at(2);
54 assert(r2 == 3.5);
55
56 try { (void) c.at(3); }
57 catch (const std::out_of_range &) {}
58 }
59
Eric Fiselier2decfad2015-07-18 23:56:04 +000060#if TEST_STD_VER > 11
Marshall Clow8bf1f082013-07-17 18:25:36 +000061 {
62 typedef double T;
63 typedef std::array<T, 3> C;
64 constexpr C c = {1, 2, 3.5};
65
66 constexpr T t1 = c.at(0);
67 static_assert (t1 == 1, "");
68
69 constexpr T t2 = c.at(2);
70 static_assert (t2 == 3.5, "");
71 }
72#endif
73
74}