xref: /llvm-project/libcxx/test/std/containers/sequences/array/array.data/data_const.pass.cpp (revision afeda5c251f9a4696ab9178ecc6d4be7af72c105)
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 // const T* data() const;
13 
14 #include <array>
15 #include <cassert>
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 int main()
24 {
25     {
26         typedef double T;
27         typedef std::array<T, 3> C;
28         const C c = {1, 2, 3.5};
29         const T* p = c.data();
30         assert(p[0] == 1);
31         assert(p[1] == 2);
32         assert(p[2] == 3.5);
33     }
34     {
35         typedef double T;
36         typedef std::array<T, 0> C;
37         const C c = {};
38         const T* p = c.data();
39         (void)p; // to placate scan-build
40     }
41     {
42       struct NoDefault {
43         NoDefault(int) {}
44       };
45       typedef NoDefault T;
46       typedef std::array<T, 0> C;
47       const C c = {};
48       const T* p = c.data();
49       assert(p != nullptr);
50     }
51 #if TEST_STD_VER > 14
52     {
53         typedef std::array<int, 5> C;
54         constexpr C c1{0,1,2,3,4};
55         constexpr const C c2{0,1,2,3,4};
56 
57         static_assert (  c1.data()  == &c1[0], "");
58         static_assert ( *c1.data()  ==  c1[0], "");
59         static_assert (  c2.data()  == &c2[0], "");
60         static_assert ( *c2.data()  ==  c2[0], "");
61     }
62 #endif
63 }
64