data_const.pass.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // <array>
  10. // const T* data() const;
  11. #include <array>
  12. #include <cassert>
  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()
  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. 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. 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. }