• 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       typedef double T;
69       typedef std::array<T, 0> C;
70       C c = {};
71       C const& cc = c;
72       static_assert((std::is_same<decltype(c.front()), T &>::value), "");
73       static_assert((std::is_same<decltype(cc.front()), const T &>::value), "");
74       static_assert((std::is_same<decltype(c.back()), T &>::value), "");
75       static_assert((std::is_same<decltype(cc.back()), const T &>::value), "");
76       if (c.size() > (0)) { // always false
77         TEST_IGNORE_NODISCARD c.front();
78         TEST_IGNORE_NODISCARD c.back();
79         TEST_IGNORE_NODISCARD cc.front();
80         TEST_IGNORE_NODISCARD cc.back();
81       }
82     }
83     {
84       typedef double T;
85       typedef std::array<const T, 0> C;
86       C c = {{}};
87       C const& cc = c;
88       static_assert((std::is_same<decltype(c.front()),  const T &>::value), "");
89       static_assert((std::is_same<decltype(cc.front()), const T &>::value), "");
90       static_assert((std::is_same<decltype(c.back()),   const T &>::value), "");
91       static_assert((std::is_same<decltype(cc.back()),  const T &>::value), "");
92       if (c.size() > (0)) {
93         TEST_IGNORE_NODISCARD c.front();
94         TEST_IGNORE_NODISCARD c.back();
95         TEST_IGNORE_NODISCARD cc.front();
96         TEST_IGNORE_NODISCARD cc.back();
97       }
98     }
99 #if TEST_STD_VER > 11
100     {
101         typedef double T;
102         typedef std::array<T, 3> C;
103         constexpr C c = {1, 2, 3.5};
104 
105         constexpr T t1 = c.front();
106         static_assert (t1 == 1, "");
107 
108         constexpr T t2 = c.back();
109         static_assert (t2 == 3.5, "");
110     }
111 #endif
112 
113 #if TEST_STD_VER > 14
114     {
115         static_assert (check_front(1),   "");
116         static_assert (check_back (3.5), "");
117     }
118 #endif
119 }
120