implicit_copy.pass.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // implicitly generated array constructors / assignment operators
  10. #include <array>
  11. #include <type_traits>
  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. // In C++03 the copy assignment operator is not deleted when the implicitly
  18. // generated operator would be ill-formed; like in the case of a struct with a
  19. // const member.
  20. #if TEST_STD_VER < 11
  21. #define TEST_NOT_COPY_ASSIGNABLE(T) ((void)0)
  22. #else
  23. #define TEST_NOT_COPY_ASSIGNABLE(T) static_assert(!std::is_copy_assignable<T>::value, "")
  24. #endif
  25. struct NoDefault {
  26. NoDefault(int) {}
  27. };
  28. int main(int, char**) {
  29. {
  30. typedef double T;
  31. typedef std::array<T, 3> C;
  32. C c = {1.1, 2.2, 3.3};
  33. C c2 = c;
  34. c2 = c;
  35. static_assert(std::is_copy_constructible<C>::value, "");
  36. static_assert(std::is_copy_assignable<C>::value, "");
  37. }
  38. {
  39. typedef double T;
  40. typedef std::array<const T, 3> C;
  41. C c = {1.1, 2.2, 3.3};
  42. C c2 = c;
  43. ((void)c2);
  44. static_assert(std::is_copy_constructible<C>::value, "");
  45. TEST_NOT_COPY_ASSIGNABLE(C);
  46. }
  47. {
  48. typedef double T;
  49. typedef std::array<T, 0> C;
  50. C c = {};
  51. C c2 = c;
  52. c2 = c;
  53. static_assert(std::is_copy_constructible<C>::value, "");
  54. static_assert(std::is_copy_assignable<C>::value, "");
  55. }
  56. {
  57. // const arrays of size 0 should disable the implicit copy assignment operator.
  58. typedef double T;
  59. typedef std::array<const T, 0> C;
  60. C c = {{}};
  61. C c2 = c;
  62. ((void)c2);
  63. static_assert(std::is_copy_constructible<C>::value, "");
  64. TEST_NOT_COPY_ASSIGNABLE(C);
  65. }
  66. {
  67. typedef NoDefault T;
  68. typedef std::array<T, 0> C;
  69. C c = {};
  70. C c2 = c;
  71. c2 = c;
  72. static_assert(std::is_copy_constructible<C>::value, "");
  73. static_assert(std::is_copy_assignable<C>::value, "");
  74. }
  75. {
  76. typedef NoDefault T;
  77. typedef std::array<const T, 0> C;
  78. C c = {{}};
  79. C c2 = c;
  80. ((void)c2);
  81. static_assert(std::is_copy_constructible<C>::value, "");
  82. TEST_NOT_COPY_ASSIGNABLE(C);
  83. }
  84. return 0;
  85. }