assign.move.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/config.hpp>
  6. #include <boost/hana/first.hpp>
  7. #include <boost/hana/pair.hpp>
  8. #include <boost/hana/second.hpp>
  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 = 1) : 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. constexpr auto in_constexpr_context(int a, short b) {
  26. hana::pair<int, short> p1(a, b);
  27. hana::pair<int, short> p2;
  28. hana::pair<double, long> p3;
  29. p2 = std::move(p1);
  30. p3 = std::move(p2);
  31. return p3;
  32. }
  33. int main() {
  34. // from pair<T, U> to pair<T, U>
  35. {
  36. hana::pair<MoveOnly, short> p1(MoveOnly{3}, 4);
  37. hana::pair<MoveOnly, short> p2;
  38. p2 = std::move(p1);
  39. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == MoveOnly{3});
  40. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4);
  41. }
  42. // from pair<T, U> to pair<V, W>
  43. {
  44. hana::pair<MoveOnlyDerived, short> p1(MoveOnlyDerived{3}, 4);
  45. hana::pair<MoveOnly, long> p2;
  46. p2 = std::move(p1);
  47. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == MoveOnly{3});
  48. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4);
  49. }
  50. // make sure that also works in a constexpr context
  51. // (test fails under GCC <= 6 due to buggy constexpr)
  52. #if BOOST_HANA_CONFIG_GCC >= BOOST_HANA_CONFIG_VERSION(7, 0, 0)
  53. {
  54. constexpr auto p = in_constexpr_context(3, 4);
  55. static_assert(hana::first(p) == 3, "");
  56. static_assert(hana::second(p) == 4, "");
  57. }
  58. #endif
  59. }