make_shared_move_emulation_test.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // make_shared_move_emulation_test.cpp - a test of make_shared
  2. // semi-perfect forwarding of constructor arguments when using a C++03
  3. // compiler with move emulation.
  4. // Note the "semi": it means moving temporaries (real r-values) doesn't work.
  5. //
  6. // Copyright 2016 Giel van Schijndel
  7. //
  8. // Distributed under the Boost Software License, Version 1.0.
  9. // See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/make_shared.hpp>
  13. #include <boost/move/core.hpp>
  14. #include <boost/move/utility_core.hpp>
  15. #include <boost/shared_ptr.hpp>
  16. class movearg
  17. {
  18. private:
  19. BOOST_MOVABLE_BUT_NOT_COPYABLE(movearg)
  20. public:
  21. movearg()
  22. {}
  23. movearg(BOOST_RV_REF(movearg))
  24. {}
  25. movearg& operator=(BOOST_RV_REF(movearg))
  26. {
  27. return *this;
  28. }
  29. };
  30. class ByVal
  31. {
  32. public:
  33. ByVal(movearg) {}
  34. };
  35. class ByRef
  36. {
  37. public:
  38. enum constructor_id
  39. {
  40. move_constructor,
  41. const_ref_constructor
  42. };
  43. ByRef(BOOST_RV_REF(movearg)): constructed_by_(move_constructor)
  44. {}
  45. ByRef(const movearg &arg): constructed_by_(const_ref_constructor)
  46. {}
  47. constructor_id constructed_by_;
  48. };
  49. int main()
  50. {
  51. {
  52. movearg a;
  53. boost::shared_ptr< ByVal > x = boost::make_shared< ByVal >(boost::move(a));
  54. }
  55. {
  56. movearg a;
  57. boost::shared_ptr< ByRef > x = boost::make_shared< ByRef >(boost::move(a));
  58. BOOST_TEST( x->constructed_by_ == ByRef::move_constructor);
  59. }
  60. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  61. {
  62. boost::shared_ptr< ByVal > x = boost::make_shared< ByVal >(movearg());
  63. boost::shared_ptr< ByRef > y = boost::make_shared< ByRef >(movearg());
  64. BOOST_TEST( y->constructed_by_ == ByRef::move_constructor);
  65. }
  66. #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  67. {
  68. const movearg ca;
  69. boost::shared_ptr< ByRef > x = boost::make_shared< ByRef >(ca);
  70. BOOST_TEST( x->constructed_by_ == ByRef::const_ref_constructor);
  71. }
  72. return boost::report_errors();
  73. }