make_shared_perfect_forwarding_test.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // make_shared_perfect_forwarding_test.cpp - a test of make_shared
  2. // perfect forwarding of constructor arguments when using a C++0x
  3. // compiler.
  4. //
  5. // Copyright 2009 Frank Mori Hess
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/make_shared.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #if defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  14. int main()
  15. {
  16. return 0;
  17. }
  18. #else // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  19. class myarg
  20. {
  21. public:
  22. myarg()
  23. {}
  24. private:
  25. myarg(myarg && other)
  26. {}
  27. myarg& operator=(myarg && other)
  28. {
  29. return *this;
  30. }
  31. myarg(const myarg & other)
  32. {}
  33. myarg& operator=(const myarg & other)
  34. {
  35. return *this;
  36. }
  37. };
  38. class X
  39. {
  40. public:
  41. enum constructor_id
  42. {
  43. move_constructor,
  44. const_ref_constructor,
  45. ref_constructor
  46. };
  47. X(myarg &&arg): constructed_by_(move_constructor)
  48. {}
  49. X(const myarg &arg): constructed_by_(const_ref_constructor)
  50. {}
  51. X(myarg &arg): constructed_by_(ref_constructor)
  52. {}
  53. constructor_id constructed_by_;
  54. };
  55. struct Y
  56. {
  57. Y(int &value): ref(value)
  58. {}
  59. int &ref;
  60. };
  61. int main()
  62. {
  63. {
  64. myarg a;
  65. boost::shared_ptr< X > x = boost::make_shared< X >(a);
  66. BOOST_TEST( x->constructed_by_ == X::ref_constructor);
  67. }
  68. {
  69. const myarg ca;
  70. boost::shared_ptr< X > x = boost::make_shared< X >(ca);
  71. BOOST_TEST( x->constructed_by_ == X::const_ref_constructor);
  72. }
  73. {
  74. boost::shared_ptr< X > x = boost::make_shared< X >(myarg());
  75. BOOST_TEST( x->constructed_by_ == X::move_constructor);
  76. }
  77. {
  78. int value = 1;
  79. boost::shared_ptr< Y > y = boost::make_shared< Y >(value);
  80. BOOST_TEST( y->ref == 1 && value == y->ref );
  81. ++y->ref;
  82. BOOST_TEST( value == y->ref );
  83. }
  84. return boost::report_errors();
  85. }
  86. #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )