define_tpl_struct_move.cpp 1.7 KB

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