define_assoc_tpl_struct_move.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*=============================================================================
  2. Copyright (c) 2016 Kohei Takahashi
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <boost/config.hpp>
  7. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/fusion/adapted/struct/define_assoc_struct.hpp>
  10. #include <utility>
  11. struct key_type;
  12. struct wrapper
  13. {
  14. int value;
  15. wrapper() : value(42) {}
  16. wrapper(wrapper&& other) : value(other.value) { other.value = 0; }
  17. wrapper(wrapper const& other) : value(other.value) {}
  18. wrapper& operator=(wrapper&& other) { value = other.value; other.value = 0; return *this; }
  19. wrapper& operator=(wrapper const& other) { value = other.value; return *this; }
  20. };
  21. BOOST_FUSION_DEFINE_ASSOC_TPL_STRUCT((W), (ns), value, (W, w, key_type))
  22. int main()
  23. {
  24. using namespace boost::fusion;
  25. {
  26. ns::value<wrapper> x;
  27. ns::value<wrapper> y(x); // copy
  28. BOOST_TEST(x.w.value == 42);
  29. BOOST_TEST(y.w.value == 42);
  30. ++y.w.value;
  31. BOOST_TEST(x.w.value == 42);
  32. BOOST_TEST(y.w.value == 43);
  33. y = x; // copy assign
  34. BOOST_TEST(x.w.value == 42);
  35. BOOST_TEST(y.w.value == 42);
  36. }
  37. {
  38. ns::value<wrapper> x;
  39. ns::value<wrapper> y(std::move(x)); // move
  40. BOOST_TEST(x.w.value == 0);
  41. BOOST_TEST(y.w.value == 42);
  42. ++y.w.value;
  43. BOOST_TEST(x.w.value == 0);
  44. BOOST_TEST(y.w.value == 43);
  45. y = std::move(x); // move assign
  46. BOOST_TEST(x.w.value == 0);
  47. BOOST_TEST(y.w.value == 0);
  48. }
  49. return boost::report_errors();
  50. }
  51. #else
  52. int main()
  53. {
  54. }
  55. #endif