xref: /aosp_15_r20/external/libcxx/test/std/containers/sequences/array/array.data/data.pass.cpp (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
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 // T *data();
13 
14 #include <array>
15 #include <cassert>
16 #include <cstddef>       // for std::max_align_t
17 
18 #include "test_macros.h"
19 
20 // std::array is explicitly allowed to be initialized with A a = { init-list };.
21 // Disable the missing braces warning for this reason.
22 #include "disable_missing_braces_warning.h"
23 
24 struct NoDefault {
NoDefaultNoDefault25   NoDefault(int) {}
26 };
27 
28 
main()29 int main()
30 {
31     {
32         typedef double T;
33         typedef std::array<T, 3> C;
34         C c = {1, 2, 3.5};
35         T* p = c.data();
36         assert(p[0] == 1);
37         assert(p[1] == 2);
38         assert(p[2] == 3.5);
39     }
40     {
41         typedef double T;
42         typedef std::array<T, 0> C;
43         C c = {};
44         T* p = c.data();
45         LIBCPP_ASSERT(p != nullptr);
46     }
47     {
48       typedef double T;
49       typedef std::array<const T, 0> C;
50       C c = {{}};
51       const T* p = c.data();
52       static_assert((std::is_same<decltype(c.data()), const T*>::value), "");
53       LIBCPP_ASSERT(p != nullptr);
54     }
55   {
56       typedef std::max_align_t T;
57       typedef std::array<T, 0> C;
58       const C c = {};
59       const T* p = c.data();
60       LIBCPP_ASSERT(p != nullptr);
61       std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
62       assert(pint % TEST_ALIGNOF(std::max_align_t) == 0);
63     }
64     {
65       typedef NoDefault T;
66       typedef std::array<T, 0> C;
67       C c = {};
68       T* p = c.data();
69       LIBCPP_ASSERT(p != nullptr);
70     }
71 }
72