blob: c271788f36f6a9ef14e2f9fa643ca2cf836f0a5e [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
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 Fiselier2decfad2015-07-18 23:56:04 +000020#include "test_macros.h"
21
Eric Fiselierb4e2e7a2015-10-01 07:05:38 +000022// 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 Fiselier2decfad2015-07-18 23:56:04 +000025
Marshall Clow8bf1f082013-07-17 18:25:36 +000026int 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 Fiselier2decfad2015-07-18 23:56:04 +000036
Marshall Clow8bf1f082013-07-17 18:25:36 +000037 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 Fiselier2decfad2015-07-18 23:56:04 +000059#if TEST_STD_VER > 11
Marshall Clow8bf1f082013-07-17 18:25:36 +000060 {
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}