ref_ref_test.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Test that ref(ref(x)) does NOT collapse to ref(x)
  3. //
  4. // This irregularity of std::ref is questionable and breaks
  5. // existing Boost code such as proto::make_expr
  6. //
  7. // Copyright 2014 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0.
  10. // See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt
  12. //
  13. #include <boost/ref.hpp>
  14. #include <boost/core/lightweight_test.hpp>
  15. template<class T> void test( T const & t )
  16. {
  17. {
  18. boost::reference_wrapper< T const > r = boost::ref( t );
  19. BOOST_TEST_EQ( &r.get(), &t );
  20. }
  21. {
  22. boost::reference_wrapper< T const > r = boost::cref( t );
  23. BOOST_TEST_EQ( &r.get(), &t );
  24. }
  25. }
  26. template<class T> void test_nonconst( T & t )
  27. {
  28. {
  29. boost::reference_wrapper< T > r = boost::ref( t );
  30. BOOST_TEST_EQ( &r.get(), &t );
  31. }
  32. {
  33. boost::reference_wrapper< T const > r = boost::cref( t );
  34. BOOST_TEST_EQ( &r.get(), &t );
  35. }
  36. }
  37. int main()
  38. {
  39. int x = 0;
  40. test( x );
  41. test( boost::ref( x ) );
  42. test( boost::cref( x ) );
  43. test_nonconst( x );
  44. {
  45. boost::reference_wrapper< int > r = boost::ref( x );
  46. test_nonconst( r );
  47. }
  48. {
  49. boost::reference_wrapper< int const > r = boost::cref( x );
  50. test_nonconst( r );
  51. }
  52. return boost::report_errors();
  53. }