1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <array>
10 
11 // T *data();
12 
13 #include <array>
14 #include <cassert>
15 #include <cstddef>       // for std::max_align_t
16 
17 #include "test_macros.h"
18 
19 struct NoDefault {
20     TEST_CONSTEXPR NoDefault(int) { }
21 };
22 
23 #if TEST_STD_VER < 11
24 struct natural_alignment {
25     long t1;
26     long long t2;
27     double t3;
28     long double t4;
29 };
30 #endif
31 
32 TEST_CONSTEXPR_CXX17 bool tests()
33 {
34     {
35         typedef double T;
36         typedef std::array<T, 3> C;
37         C c = {1, 2, 3.5};
38         T* p = c.data();
39         assert(p[0] == 1);
40         assert(p[1] == 2);
41         assert(p[2] == 3.5);
42     }
43     {
44         typedef double T;
45         typedef std::array<T, 0> C;
46         C c = {};
47         T* p = c.data();
48         (void)p;
49     }
50     {
51         typedef double T;
52         typedef std::array<const T, 0> C;
53         C c = {{}};
54         const T* p = c.data();
55         (void)p;
56         static_assert((std::is_same<decltype(c.data()), const T*>::value), "");
57     }
58     {
59         typedef NoDefault T;
60         typedef std::array<T, 0> C;
61         C c = {};
62         T* p = c.data();
63         (void)p;
64     }
65     {
66         std::array<int, 5> c = {0, 1, 2, 3, 4};
67         assert(c.data() == &c[0]);
68         assert(*c.data() == c[0]);
69     }
70 
71     return true;
72 }
73 
74 int main(int, char**)
75 {
76     tests();
77 #if TEST_STD_VER >= 17
78     static_assert(tests(), "");
79 #endif
80 
81     // Test the alignment of data()
82     {
83 #if TEST_STD_VER < 11
84         typedef natural_alignment T;
85 #else
86         typedef std::max_align_t T;
87 #endif
88         typedef std::array<T, 0> C;
89         const C c = {};
90         const T* p = c.data();
91         std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
92         assert(pint % TEST_ALIGNOF(T) == 0);
93     }
94     return 0;
95 }
96