assign_pair.pass.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // UNSUPPORTED: c++98, c++03
  10. // <utility>
  11. // template <class T1, class T2> struct pair
  12. // pair& operator=(pair const& p);
  13. #include <utility>
  14. #include <memory>
  15. #include <cassert>
  16. struct NonAssignable {
  17. NonAssignable& operator=(NonAssignable const&) = delete;
  18. NonAssignable& operator=(NonAssignable&&) = delete;
  19. };
  20. struct CopyAssignable {
  21. CopyAssignable() = default;
  22. CopyAssignable(CopyAssignable const&) = default;
  23. CopyAssignable& operator=(CopyAssignable const&) = default;
  24. CopyAssignable& operator=(CopyAssignable&&) = delete;
  25. };
  26. struct MoveAssignable {
  27. MoveAssignable() = default;
  28. MoveAssignable& operator=(MoveAssignable const&) = delete;
  29. MoveAssignable& operator=(MoveAssignable&&) = default;
  30. };
  31. struct CountAssign {
  32. static int copied;
  33. static int moved;
  34. static void reset() { copied = moved = 0; }
  35. CountAssign() = default;
  36. CountAssign& operator=(CountAssign const&) { ++copied; return *this; }
  37. CountAssign& operator=(CountAssign&&) { ++moved; return *this; }
  38. };
  39. int CountAssign::copied = 0;
  40. int CountAssign::moved = 0;
  41. int main()
  42. {
  43. {
  44. typedef std::pair<CopyAssignable, short> P;
  45. const P p1(CopyAssignable(), 4);
  46. P p2;
  47. p2 = p1;
  48. assert(p2.second == 4);
  49. }
  50. {
  51. using P = std::pair<int&, int&&>;
  52. int x = 42;
  53. int y = 101;
  54. int x2 = -1;
  55. int y2 = 300;
  56. P p1(x, std::move(y));
  57. P p2(x2, std::move(y2));
  58. p1 = p2;
  59. assert(p1.first == x2);
  60. assert(p1.second == y2);
  61. }
  62. {
  63. using P = std::pair<int, NonAssignable>;
  64. static_assert(!std::is_copy_assignable<P>::value, "");
  65. }
  66. {
  67. CountAssign::reset();
  68. using P = std::pair<CountAssign, CopyAssignable>;
  69. static_assert(std::is_copy_assignable<P>::value, "");
  70. P p;
  71. P p2;
  72. p = p2;
  73. assert(CountAssign::copied == 1);
  74. assert(CountAssign::moved == 0);
  75. }
  76. {
  77. using P = std::pair<int, MoveAssignable>;
  78. static_assert(!std::is_copy_assignable<P>::value, "");
  79. }
  80. }