front_back.pass.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // reference front(); // constexpr in C++17
  11. // reference back(); // constexpr in C++17
  12. // const_reference front(); // constexpr in C++14
  13. // const_reference back(); // constexpr in C++14
  14. #include <array>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  18. // Disable the missing braces warning for this reason.
  19. #include "disable_missing_braces_warning.h"
  20. #if TEST_STD_VER > 14
  21. constexpr bool check_front( double val )
  22. {
  23. std::array<double, 3> arr = {1, 2, 3.5};
  24. return arr.front() == val;
  25. }
  26. constexpr bool check_back( double val )
  27. {
  28. std::array<double, 3> arr = {1, 2, 3.5};
  29. return arr.back() == val;
  30. }
  31. #endif
  32. int main()
  33. {
  34. {
  35. typedef double T;
  36. typedef std::array<T, 3> C;
  37. C c = {1, 2, 3.5};
  38. C::reference r1 = c.front();
  39. assert(r1 == 1);
  40. r1 = 5.5;
  41. assert(c[0] == 5.5);
  42. C::reference r2 = c.back();
  43. assert(r2 == 3.5);
  44. r2 = 7.5;
  45. assert(c[2] == 7.5);
  46. }
  47. {
  48. typedef double T;
  49. typedef std::array<T, 3> C;
  50. const C c = {1, 2, 3.5};
  51. C::const_reference r1 = c.front();
  52. assert(r1 == 1);
  53. C::const_reference r2 = c.back();
  54. assert(r2 == 3.5);
  55. }
  56. #if TEST_STD_VER > 11
  57. {
  58. typedef double T;
  59. typedef std::array<T, 3> C;
  60. constexpr C c = {1, 2, 3.5};
  61. constexpr T t1 = c.front();
  62. static_assert (t1 == 1, "");
  63. constexpr T t2 = c.back();
  64. static_assert (t2 == 3.5, "");
  65. }
  66. #endif
  67. #if TEST_STD_VER > 14
  68. {
  69. static_assert (check_front(1), "");
  70. static_assert (check_back (3.5), "");
  71. }
  72. #endif
  73. }