define_assoc_struct_move.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*=============================================================================
  2. Copyright (c) 2016,2018 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/detail/lightweight_test.hpp>
  7. #include <boost/fusion/adapted/struct/define_assoc_struct.hpp>
  8. #include <utility>
  9. struct key_type;
  10. struct wrapper
  11. {
  12. int value;
  13. wrapper() : value(42) {}
  14. wrapper(wrapper&& other) : value(other.value) { other.value = 0; }
  15. wrapper(wrapper const& other) : value(other.value) {}
  16. wrapper& operator=(wrapper&& other) { value = other.value; other.value = 0; return *this; }
  17. wrapper& operator=(wrapper const& other) { value = other.value; return *this; }
  18. };
  19. BOOST_FUSION_DEFINE_ASSOC_STRUCT((ns), value, (wrapper, w, key_type))
  20. int main()
  21. {
  22. using namespace boost::fusion;
  23. {
  24. ns::value x;
  25. ns::value y(x); // copy
  26. BOOST_TEST(x.w.value == 42);
  27. BOOST_TEST(y.w.value == 42);
  28. ++y.w.value;
  29. BOOST_TEST(x.w.value == 42);
  30. BOOST_TEST(y.w.value == 43);
  31. y = x; // copy assign
  32. BOOST_TEST(x.w.value == 42);
  33. BOOST_TEST(y.w.value == 42);
  34. }
  35. {
  36. ns::value x;
  37. ns::value y(std::move(x)); // move
  38. BOOST_TEST(x.w.value == 0);
  39. BOOST_TEST(y.w.value == 42);
  40. ++y.w.value;
  41. BOOST_TEST(x.w.value == 0);
  42. BOOST_TEST(y.w.value == 43);
  43. y = std::move(x); // move assign
  44. BOOST_TEST(x.w.value == 0);
  45. BOOST_TEST(y.w.value == 0);
  46. }
  47. return boost::report_errors();
  48. }