define_tpl_struct_inline_move.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_inline.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. namespace ns
  19. {
  20. BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE((W), value, (W, w))
  21. }
  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. // Older MSVCs and gcc 4.4 don't generate move ctor by default.
  38. #if !(defined(CI_SKIP_KNOWN_FAILURE) && (BOOST_WORKAROUND(BOOST_MSVC, < 1900) || BOOST_WORKAROUND(BOOST_GCC, / 100 == 404)))
  39. {
  40. ns::value<wrapper> x;
  41. ns::value<wrapper> y(std::move(x)); // move
  42. BOOST_TEST(x.w.value == 0);
  43. BOOST_TEST(y.w.value == 42);
  44. ++y.w.value;
  45. BOOST_TEST(x.w.value == 0);
  46. BOOST_TEST(y.w.value == 43);
  47. y = std::move(x); // move assign
  48. BOOST_TEST(x.w.value == 0);
  49. BOOST_TEST(y.w.value == 0);
  50. }
  51. #endif // !(ci && (msvc < 14.0 || gcc 4.4.x))
  52. return boost::report_errors();
  53. }