libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

data_const.pass.cpp (1951B)


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