xref: /llvm-project/libcxx/test/std/containers/sequences/array/array.data/data_const.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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 int main()
28 {
29     {
30         typedef double T;
31         typedef std::array<T, 3> C;
32         const C c = {1, 2, 3.5};
33         const T* p = c.data();
34         assert(p[0] == 1);
35         assert(p[1] == 2);
36         assert(p[2] == 3.5);
37     }
38     {
39         typedef double T;
40         typedef std::array<T, 0> C;
41         const C c = {};
42         const T* p = c.data();
43         (void)p; // to placate scan-build
44     }
45     {
46       typedef NoDefault T;
47       typedef std::array<T, 0> C;
48       const C c = {};
49       const T* p = c.data();
50       LIBCPP_ASSERT(p != nullptr);
51     }
52     {
53       typedef std::max_align_t T;
54       typedef std::array<T, 0> C;
55       const C c = {};
56       const T* p = c.data();
57       LIBCPP_ASSERT(p != nullptr);
58       std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
59       assert(pint % TEST_ALIGNOF(std::max_align_t) == 0);
60     }
61 #if TEST_STD_VER > 14
62     {
63         typedef std::array<int, 5> C;
64         constexpr C c1{0,1,2,3,4};
65         constexpr const C c2{0,1,2,3,4};
66 
67         static_assert (  c1.data()  == &c1[0], "");
68         static_assert ( *c1.data()  ==  c1[0], "");
69         static_assert (  c2.data()  == &c2[0], "");
70         static_assert ( *c2.data()  ==  c2[0], "");
71     }
72 #endif
73 }
74