data.pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // T *data();
  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. C c = {1, 2, 3.5};
  26. 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. C c = {};
  35. T* p = c.data();
  36. LIBCPP_ASSERT(p != nullptr);
  37. }
  38. {
  39. typedef double T;
  40. typedef std::array<const T, 0> C;
  41. C c = {{}};
  42. const T* p = c.data();
  43. static_assert((std::is_same<decltype(c.data()), const T*>::value), "");
  44. LIBCPP_ASSERT(p != nullptr);
  45. }
  46. {
  47. typedef std::max_align_t T;
  48. typedef std::array<T, 0> C;
  49. const C c = {};
  50. const T* p = c.data();
  51. LIBCPP_ASSERT(p != nullptr);
  52. std::uintptr_t pint = reinterpret_cast<std::uintptr_t>(p);
  53. assert(pint % TEST_ALIGNOF(std::max_align_t) == 0);
  54. }
  55. {
  56. typedef NoDefault T;
  57. typedef std::array<T, 0> C;
  58. C c = {};
  59. T* p = c.data();
  60. LIBCPP_ASSERT(p != nullptr);
  61. }
  62. return 0;
  63. }