construct_forward.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2009-2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #include <boost/move/detail/config_begin.hpp>
  12. #include <boost/move/utility_core.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. #include "../example/movable.hpp"
  15. #include "../example/copymovable.hpp"
  16. #include <cstdio>
  17. class non_movable
  18. {
  19. public:
  20. non_movable()
  21. {}
  22. };
  23. template<class MaybeRvalue>
  24. void catch_test(BOOST_RV_REF(MaybeRvalue) x
  25. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  26. ,typename ::boost::enable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
  27. #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
  28. )
  29. { (void)x;}
  30. template<class MaybeRvalue>
  31. void catch_test(BOOST_COPY_ASSIGN_REF(MaybeRvalue) x
  32. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  33. ,typename ::boost::enable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
  34. #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
  35. )
  36. { (void)x;}
  37. template<class MaybeRvalue>
  38. void catch_test(MaybeRvalue &x
  39. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  40. ,typename ::boost::enable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
  41. #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
  42. )
  43. { (void)x;}
  44. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  45. template<class MaybeRvalue>
  46. void catch_test(const MaybeRvalue& x
  47. ,typename ::boost::disable_if< ::boost::has_move_emulation_enabled<MaybeRvalue> >::type* = 0
  48. )
  49. { (void)x;}
  50. #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
  51. movable create_movable()
  52. { return movable(); }
  53. copy_movable create_copy_movable()
  54. { return copy_movable(); }
  55. non_movable create_non_movable()
  56. { return non_movable(); }
  57. void catch_test()
  58. {
  59. movable m;
  60. const movable constm;
  61. catch_test<movable>(boost::move(m));
  62. #ifdef BOOST_CATCH_CONST_RLVALUE
  63. catch_test<movable>(create_movable());
  64. #endif
  65. catch_test<movable>(m);
  66. catch_test<movable>(constm);
  67. copy_movable cm;
  68. const copy_movable constcm;
  69. catch_test<copy_movable>(boost::move(cm));
  70. catch_test<copy_movable>(create_copy_movable());
  71. catch_test<copy_movable>(cm);
  72. catch_test<copy_movable>(constcm);
  73. non_movable nm;
  74. const non_movable constnm;
  75. catch_test<non_movable>(boost::move(nm));
  76. catch_test<non_movable>(create_non_movable());
  77. catch_test<non_movable>(nm);
  78. catch_test<non_movable>(constnm);
  79. }
  80. template<class MaybeMovableOnly, class MaybeRvalue>
  81. void function_construct(BOOST_FWD_REF(MaybeRvalue) x)
  82. {
  83. //Moves in case Convertible is boost::rv<movable> copies otherwise
  84. //For C++0x perfect forwarding
  85. MaybeMovableOnly m(boost::forward<MaybeRvalue>(x));
  86. }
  87. void forward_test()
  88. {
  89. movable m;
  90. function_construct<movable>(boost::move(m));
  91. // non_movable nm;
  92. // function_construct<non_movable>(boost::move(nm));
  93. // const non_movable cnm;
  94. // function_construct<non_movable>(cnm);
  95. }
  96. int main()
  97. {
  98. catch_test();
  99. forward_test();
  100. return 0;
  101. }
  102. #include <boost/move/detail/config_end.hpp>