assign.move.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/tuple.hpp>
  6. #include <utility>
  7. namespace hana = boost::hana;
  8. struct MoveOnly {
  9. int data_;
  10. MoveOnly(MoveOnly const&) = delete;
  11. MoveOnly& operator=(MoveOnly const&) = delete;
  12. MoveOnly(int data = 1) : data_(data) { }
  13. MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; }
  14. MoveOnly& operator=(MoveOnly&& x)
  15. { data_ = x.data_; x.data_ = 0; return *this; }
  16. int get() const {return data_;}
  17. bool operator==(const MoveOnly& x) const { return data_ == x.data_; }
  18. bool operator< (const MoveOnly& x) const { return data_ < x.data_; }
  19. };
  20. int main() {
  21. {
  22. using T = hana::tuple<>;
  23. T t0;
  24. T t;
  25. t = std::move(t0);
  26. }
  27. {
  28. using T = hana::tuple<MoveOnly>;
  29. T t0(MoveOnly(0));
  30. T t;
  31. t = std::move(t0);
  32. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  33. }
  34. {
  35. using T = hana::tuple<MoveOnly, MoveOnly>;
  36. T t0(MoveOnly(0), MoveOnly(1));
  37. T t;
  38. t = std::move(t0);
  39. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  40. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1);
  41. }
  42. {
  43. using T = hana::tuple<MoveOnly, MoveOnly, MoveOnly>;
  44. T t0(MoveOnly(0), MoveOnly(1), MoveOnly(2));
  45. T t;
  46. t = std::move(t0);
  47. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  48. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1);
  49. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 2);
  50. }
  51. }