xref: /llvm-project/libcxx/test/std/containers/sequences/array/array.data/data_const.pass.cpp (revision 98f77828a98fd05760987598db1355cf08b643bd)
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 // const T* data() const;
12 
13 #include <array>
14 #include <cassert>
15 #include <cstddef>       // for std::max_align_t
16 
17 #include "test_macros.h"
18 
19 // std::array is explicitly allowed to be initialized with A a = { init-list };.
20 // Disable the missing braces warning for this reason.
21 #include "disable_missing_braces_warning.h"
22 
23 struct NoDefault {
24   NoDefault(int) {}
25 };
26 
27 #if TEST_STD_VER < 11
28 struct natural_alignment {
29     long t1;
30     long long t2;
31     double t3;
32     long double t4;
33 };
34 #endif
35 
36 int main(int, char**)
37 {
38     {
39         typedef double T;
40         typedef std::array<T, 3> C;
41         const C c = {1, 2, 3.5};
42         const T* p = c.data();
43         assert(p[0] == 1);
44         assert(p[1] == 2);
45         assert(p[2] == 3.5);
46     }
47     {
48         typedef double T;
49         typedef std::array<T, 0> C;
50         const C c = {};
51         const T* p = c.data();
52         (void)p; // to placate scan-build
53     }
54     {
55       typedef NoDefault T;
56       typedef std::array<T, 0> C;
57       const C c = {};
58       const T* p = c.data();
59       LIBCPP_ASSERT(p != nullptr);
60     }
61     {
62 #if TEST_STD_VER < 11
63       typedef natural_alignment T;
64 #else
65       typedef std::max_align_t T;
66 #endif
67       typedef std::array<T, 0> C;
68       const C c = {};
69       const T* p = c.data();
70       LIBCPP_ASSERT(p != nullptr);
71       std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
72       assert(pint % TEST_ALIGNOF(T) == 0);
73     }
74 #if TEST_STD_VER > 14
75     {
76         typedef std::array<int, 5> C;
77         constexpr C c1{0,1,2,3,4};
78         constexpr const C c2{0,1,2,3,4};
79 
80         static_assert (  c1.data()  == &c1[0], "");
81         static_assert ( *c1.data()  ==  c1[0], "");
82         static_assert (  c2.data()  == &c2[0], "");
83         static_assert ( *c2.data()  ==  c2[0], "");
84     }
85 #endif
86 
87   return 0;
88 }
89