diff --git a/include/boost/core/ref.hpp b/include/boost/core/ref.hpp index d258c76..47dc858 100644 --- a/include/boost/core/ref.hpp +++ b/include/boost/core/ref.hpp @@ -141,15 +141,6 @@ template BOOST_FORCEINLINE reference_wrapper BOOST_REF_CONST ref( T #endif } -/** - @return `ref(t.get())` - @remark Does not throw. -*/ -template BOOST_FORCEINLINE reference_wrapper BOOST_REF_CONST ref( reference_wrapper t ) -{ - return t; -} - // cref /** @@ -161,15 +152,6 @@ template BOOST_FORCEINLINE reference_wrapper BOOST_REF_CONST c return reference_wrapper(t); } -/** - @return `cref(t.get())` - @remark Does not throw. -*/ -template BOOST_FORCEINLINE reference_wrapper BOOST_REF_CONST cref( reference_wrapper t ) -{ - return reference_wrapper(t.get()); -} - #undef BOOST_REF_CONST #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) diff --git a/test/ref_ref_test.cpp b/test/ref_ref_test.cpp index b667a7f..46be561 100644 --- a/test/ref_ref_test.cpp +++ b/test/ref_ref_test.cpp @@ -1,5 +1,8 @@ // -// Test for ref(ref(x)) +// Test that ref(ref(x)) does NOT collapse to ref(x) +// +// This irregularity of std::ref is questionable and breaks +// existing Boost code such as proto::make_expr // // Copyright 2014 Peter Dimov // @@ -11,28 +14,50 @@ #include #include +template void test( T const & t ) +{ + { + boost::reference_wrapper< T const > r = boost::ref( t ); + BOOST_TEST_EQ( &r.get(), &t ); + } + + { + boost::reference_wrapper< T const > r = boost::cref( t ); + BOOST_TEST_EQ( &r.get(), &t ); + } +} + +template void test_nonconst( T & t ) +{ + { + boost::reference_wrapper< T > r = boost::ref( t ); + BOOST_TEST_EQ( &r.get(), &t ); + } + + { + boost::reference_wrapper< T const > r = boost::cref( t ); + BOOST_TEST_EQ( &r.get(), &t ); + } +} + int main() { int x = 0; + test( x ); + test( boost::ref( x ) ); + test( boost::cref( x ) ); + + test_nonconst( x ); + { - boost::reference_wrapper< int > r = boost::ref( boost::ref( x ) ); - BOOST_TEST_EQ( &r.get(), &x ); + boost::reference_wrapper< int > r = boost::ref( x ); + test_nonconst( r ); } { - boost::reference_wrapper< int const > r = boost::ref( boost::cref( x ) ); - BOOST_TEST_EQ( &r.get(), &x ); - } - - { - boost::reference_wrapper< int const > r = boost::cref( boost::ref( x ) ); - BOOST_TEST_EQ( &r.get(), &x ); - } - - { - boost::reference_wrapper< int const > r = boost::cref( boost::cref( x ) ); - BOOST_TEST_EQ( &r.get(), &x ); + boost::reference_wrapper< int const > r = boost::cref( x ); + test_nonconst( r ); } return boost::report_errors();