cnstr.move.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/bool.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/fwd/hash.hpp>
  8. #include <boost/hana/map.hpp>
  9. #include <boost/hana/pair.hpp>
  10. #include <boost/hana/type.hpp>
  11. #include <support/constexpr_move_only.hpp>
  12. #include <support/tracked_move_only.hpp>
  13. #include <string>
  14. #include <type_traits>
  15. #include <utility>
  16. namespace hana = boost::hana;
  17. constexpr bool in_constexpr_context() {
  18. auto t0 = hana::make_map(
  19. hana::make_pair(ConstexprMoveOnly<2>{}, ConstexprMoveOnly<20>{}),
  20. hana::make_pair(ConstexprMoveOnly<3>{}, ConstexprMoveOnly<30>{}));
  21. auto t_implicit = std::move(t0);
  22. auto t_explicit(std::move(t_implicit));
  23. (void)t_implicit;
  24. (void)t_explicit;
  25. return true;
  26. }
  27. static_assert(in_constexpr_context(), "");
  28. struct NoMove {
  29. NoMove() = default;
  30. NoMove(NoMove const&) = delete;
  31. NoMove(NoMove&&) = delete;
  32. friend auto operator==(NoMove const&, NoMove const&) { return hana::true_c; }
  33. friend auto operator!=(NoMove const&, NoMove const&) { return hana::false_c; }
  34. };
  35. // Note: It is also useful to check with a non-empty class, because that
  36. // triggers different instantiations due to EBO.
  37. struct NoMove_nonempty {
  38. NoMove_nonempty() = default;
  39. NoMove_nonempty(NoMove_nonempty const&) = delete;
  40. NoMove_nonempty(NoMove_nonempty&&) = delete;
  41. int i;
  42. friend auto operator==(NoMove_nonempty const&, NoMove_nonempty const&) { return hana::true_c; }
  43. friend auto operator!=(NoMove_nonempty const&, NoMove_nonempty const&) { return hana::false_c; }
  44. };
  45. namespace boost { namespace hana {
  46. template <>
  47. struct hash_impl<NoMove> {
  48. static constexpr auto apply(NoMove const&)
  49. { return hana::type_c<NoMove>; };
  50. };
  51. template <>
  52. struct hash_impl<NoMove_nonempty> {
  53. static constexpr auto apply(NoMove_nonempty const&)
  54. { return hana::type_c<NoMove_nonempty>; };
  55. };
  56. }}
  57. int main() {
  58. {
  59. auto t0 = hana::make_map();
  60. auto t_implicit = std::move(t0);
  61. auto t_explicit(std::move(t_implicit));
  62. (void)t_explicit;
  63. (void)t_implicit;
  64. }
  65. {
  66. auto t0 = hana::make_map(hana::make_pair(TrackedMoveOnly<1>{}, TrackedMoveOnly<10>{}));
  67. auto t_implicit = std::move(t0);
  68. auto t_explicit(std::move(t_implicit));
  69. (void)t_implicit;
  70. (void)t_explicit;
  71. }
  72. {
  73. auto t0 = hana::make_map(hana::make_pair(TrackedMoveOnly<1>{}, TrackedMoveOnly<10>{}),
  74. hana::make_pair(TrackedMoveOnly<2>{}, TrackedMoveOnly<20>{}));
  75. auto t_implicit = std::move(t0);
  76. auto t_explicit(std::move(t_implicit));
  77. (void)t_implicit;
  78. (void)t_explicit;
  79. }
  80. {
  81. auto t0 = hana::make_map(hana::make_pair(TrackedMoveOnly<1>{}, TrackedMoveOnly<10>{}),
  82. hana::make_pair(TrackedMoveOnly<2>{}, TrackedMoveOnly<20>{}),
  83. hana::make_pair(TrackedMoveOnly<3>{}, TrackedMoveOnly<30>{}));
  84. auto t_implicit = std::move(t0);
  85. auto t_explicit(std::move(t_implicit));
  86. (void)t_implicit;
  87. (void)t_explicit;
  88. }
  89. {
  90. auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"}));
  91. auto moved = std::move(t0);
  92. BOOST_HANA_RUNTIME_CHECK(
  93. moved == hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"}))
  94. );
  95. }
  96. {
  97. using Map1 = hana::map<hana::pair<NoMove, NoMove>>;
  98. Map1 map1; (void)map1;
  99. static_assert(!std::is_move_constructible<Map1>::value, "");
  100. using Map2 = hana::map<hana::pair<NoMove_nonempty, NoMove_nonempty>>;
  101. Map2 map2; (void)map2;
  102. static_assert(!std::is_move_constructible<Map2>::value, "");
  103. }
  104. }