cnstr.move.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/first.hpp>
  6. #include <boost/hana/pair.hpp>
  7. #include <boost/hana/second.hpp>
  8. #include <type_traits>
  9. #include <utility>
  10. namespace hana = boost::hana;
  11. struct MoveOnly {
  12. int data_;
  13. MoveOnly(MoveOnly const&) = delete;
  14. MoveOnly& operator=(MoveOnly const&) = delete;
  15. MoveOnly(int data) : data_(data) { }
  16. MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; }
  17. MoveOnly& operator=(MoveOnly&& x)
  18. { data_ = x.data_; x.data_ = 0; return *this; }
  19. bool operator==(const MoveOnly& x) const { return data_ == x.data_; }
  20. };
  21. struct MoveOnlyDerived : MoveOnly {
  22. MoveOnlyDerived(MoveOnlyDerived&&) = default;
  23. MoveOnlyDerived(int data = 1) : MoveOnly(data) { }
  24. };
  25. template <typename Target>
  26. struct implicit_to {
  27. constexpr operator Target() const { return Target{}; }
  28. };
  29. struct NoMove {
  30. NoMove() = default;
  31. NoMove(NoMove const&) = delete;
  32. NoMove(NoMove&&) = delete;
  33. };
  34. // Note: It is also useful to check with a non-empty class, because that
  35. // triggers different instantiations due to EBO.
  36. struct NoMove_nonempty {
  37. NoMove_nonempty() = default;
  38. NoMove_nonempty(NoMove_nonempty const&) = delete;
  39. NoMove_nonempty(NoMove_nonempty&&) = delete;
  40. int i;
  41. };
  42. int main() {
  43. {
  44. hana::pair<MoveOnly, short> p1(MoveOnly{3}, 4);
  45. hana::pair<MoveOnly, short> p2(std::move(p1));
  46. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == MoveOnly{3});
  47. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4);
  48. }
  49. // Make sure it works across pair types
  50. {
  51. hana::pair<MoveOnlyDerived, short> p1(MoveOnlyDerived{3}, 4);
  52. hana::pair<MoveOnly, long> p2 = std::move(p1);
  53. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == MoveOnly{3});
  54. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4);
  55. }
  56. {
  57. struct target1 {
  58. target1() = default;
  59. target1(target1 const&) = delete;
  60. target1(target1&&) = default;
  61. };
  62. struct target2 {
  63. target2() = default;
  64. target2(target2 const&) = delete;
  65. target2(target2&&) = default;
  66. };
  67. using Target = hana::pair<target1, target2>;
  68. Target p1(hana::make_pair(target1{}, target2{})); (void)p1;
  69. Target p2(hana::make_pair(implicit_to<target1>{}, target2{})); (void)p2;
  70. Target p3(hana::make_pair(target1{}, implicit_to<target2>{})); (void)p3;
  71. Target p4(hana::make_pair(implicit_to<target1>{}, implicit_to<target2>{})); (void)p4;
  72. }
  73. // Make sure we don't define the move constructor when it shouldn't be defined.
  74. {
  75. using Pair1 = hana::pair<NoMove, NoMove>;
  76. Pair1 pair1; (void)pair1;
  77. static_assert(!std::is_move_constructible<Pair1>::value, "");
  78. using Pair2 = hana::pair<NoMove_nonempty, NoMove_nonempty>;
  79. Pair2 pair2; (void)pair2;
  80. static_assert(!std::is_move_constructible<Pair2>::value, "");
  81. }
  82. }