• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 front();       // constexpr in C++17
13 // reference back();        // constexpr in C++17
14 // const_reference front(); // constexpr in C++14
15 // const_reference back();  // constexpr in C++14
16 
17 #include <array>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 
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"
25 
26 #if TEST_STD_VER > 14
check_front(double val)27 constexpr bool check_front( double val )
28 {
29     std::array<double, 3> arr = {1, 2, 3.5};
30 	return arr.front() == val;
31 }
32 
check_back(double val)33 constexpr bool check_back( double val )
34 {
35     std::array<double, 3> arr = {1, 2, 3.5};
36 	return arr.back() == val;
37 }
38 #endif
39 
main()40 int main()
41 {
42     {
43         typedef double T;
44         typedef std::array<T, 3> C;
45         C c = {1, 2, 3.5};
46 
47         C::reference r1 = c.front();
48         assert(r1 == 1);
49         r1 = 5.5;
50         assert(c[0] == 5.5);
51 
52         C::reference r2 = c.back();
53         assert(r2 == 3.5);
54         r2 = 7.5;
55         assert(c[2] == 7.5);
56     }
57     {
58         typedef double T;
59         typedef std::array<T, 3> C;
60         const C c = {1, 2, 3.5};
61         C::const_reference r1 = c.front();
62         assert(r1 == 1);
63 
64         C::const_reference r2 = c.back();
65         assert(r2 == 3.5);
66     }
67 
68 #if TEST_STD_VER > 11
69     {
70         typedef double T;
71         typedef std::array<T, 3> C;
72         constexpr C c = {1, 2, 3.5};
73 
74         constexpr T t1 = c.front();
75         static_assert (t1 == 1, "");
76 
77         constexpr T t2 = c.back();
78         static_assert (t2 == 3.5, "");
79     }
80 #endif
81 
82 #if TEST_STD_VER > 14
83     {
84         static_assert (check_front(1),   "");
85         static_assert (check_back (3.5), "");
86     }
87 #endif
88 }
89