cnstr.move.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <type_traits>
  7. #include <utility>
  8. namespace hana = boost::hana;
  9. struct MoveOnly {
  10. int data_;
  11. MoveOnly(MoveOnly const&) = delete;
  12. MoveOnly& operator=(MoveOnly const&) = delete;
  13. MoveOnly(int data = 1) : data_(data) { }
  14. MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; }
  15. MoveOnly& operator=(MoveOnly&& x)
  16. { data_ = x.data_; x.data_ = 0; return *this; }
  17. int get() const {return data_;}
  18. bool operator==(const MoveOnly& x) const { return data_ == x.data_; }
  19. bool operator< (const MoveOnly& x) const { return data_ < x.data_; }
  20. };
  21. int main() {
  22. {
  23. using T = hana::tuple<>;
  24. T t0;
  25. T t = std::move(t0); (void)t;
  26. }
  27. {
  28. using T = hana::tuple<MoveOnly>;
  29. T t0(MoveOnly(0));
  30. T t = std::move(t0); (void)t;
  31. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  32. }
  33. {
  34. using T = hana::tuple<MoveOnly, MoveOnly>;
  35. T t0(MoveOnly(0), MoveOnly(1));
  36. T t = std::move(t0); (void)t;
  37. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  38. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1);
  39. }
  40. {
  41. using T = hana::tuple<MoveOnly, MoveOnly, MoveOnly>;
  42. T t0(MoveOnly(0), MoveOnly(1), MoveOnly(2));
  43. T t = std::move(t0); (void)t;
  44. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  45. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1);
  46. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 2);
  47. }
  48. {
  49. // Check for SFINAE-friendliness
  50. static_assert(!std::is_constructible<
  51. hana::tuple<MoveOnly>, MoveOnly const&
  52. >{}, "");
  53. static_assert(!std::is_constructible<
  54. hana::tuple<MoveOnly>, MoveOnly&
  55. >{}, "");
  56. static_assert(std::is_constructible<
  57. hana::tuple<MoveOnly>, MoveOnly
  58. >{}, "");
  59. static_assert(std::is_constructible<
  60. hana::tuple<MoveOnly>, MoveOnly&&
  61. >{}, "");
  62. }
  63. }