data_const.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // <array>
  9. // const T* data() const;
  10. #include <array>
  11. #include <cassert>
  12. #include <cstddef> // for std::max_align_t
  13. #include "test_macros.h"
  14. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  15. // Disable the missing braces warning for this reason.
  16. #include "disable_missing_braces_warning.h"
  17. struct NoDefault {
  18. NoDefault(int) {}
  19. };
  20. int main(int, char**)
  21. {
  22. {
  23. typedef double T;
  24. typedef std::array<T, 3> C;
  25. const C c = {1, 2, 3.5};
  26. const T* p = c.data();
  27. assert(p[0] == 1);
  28. assert(p[1] == 2);
  29. assert(p[2] == 3.5);
  30. }
  31. {
  32. typedef double T;
  33. typedef std::array<T, 0> C;
  34. const C c = {};
  35. const T* p = c.data();
  36. (void)p; // to placate scan-build
  37. }
  38. {
  39. typedef NoDefault T;
  40. typedef std::array<T, 0> C;
  41. const C c = {};
  42. const T* p = c.data();
  43. LIBCPP_ASSERT(p != nullptr);
  44. }
  45. {
  46. typedef std::max_align_t T;
  47. typedef std::array<T, 0> C;
  48. const C c = {};
  49. const T* p = c.data();
  50. LIBCPP_ASSERT(p != nullptr);
  51. std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
  52. assert(pint % TEST_ALIGNOF(std::max_align_t) == 0);
  53. }
  54. #if TEST_STD_VER > 14
  55. {
  56. typedef std::array<int, 5> C;
  57. constexpr C c1{0,1,2,3,4};
  58. constexpr const C c2{0,1,2,3,4};
  59. static_assert ( c1.data() == &c1[0], "");
  60. static_assert ( *c1.data() == c1[0], "");
  61. static_assert ( c2.data() == &c2[0], "");
  62. static_assert ( *c2.data() == c2[0], "");
  63. }
  64. #endif
  65. return 0;
  66. }