Skip to content

Commit

Permalink
Disable binding ref to temporaries when rvalue references are supported
Browse files Browse the repository at this point in the history
  • Loading branch information
K-ballo committed Jun 9, 2014
1 parent af629ff commit 45f7564
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
36 changes: 36 additions & 0 deletions include/boost/core/ref.hpp
Expand Up @@ -66,6 +66,14 @@ template<class T> class reference_wrapper
*/
BOOST_FORCEINLINE explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}

# if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
/**
@remark Construction from a temporary object is disabled.
*/
BOOST_DELETED_FUNCTION(reference_wrapper(T&& t))
public:
# endif

/**
@return The stored reference.
@remark Does not throw.
Expand Down Expand Up @@ -144,6 +152,34 @@ template<class T> BOOST_FORCEINLINE reference_wrapper<T const> BOOST_REF_CONST c

# undef BOOST_REF_CONST

# if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)

/**
@cond
*/
# if defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
# define BOOST_REF_DELETE
# else
# define BOOST_REF_DELETE = delete
# endif
/**
@endcond
*/

/**
@remark Construction from a temporary object is disabled.
*/
template<class T> void ref(T const&& t) BOOST_REF_DELETE;

/**
@remark Construction from a temporary object is disabled.
*/
template<class T> void cref(T const&& t) BOOST_REF_DELETE;

# undef BOOST_REF_DELETE

# endif

// is_reference_wrapper

/**
Expand Down
3 changes: 3 additions & 0 deletions test/Jamfile.v2
Expand Up @@ -19,6 +19,9 @@ compile-fail checked_delete_fail2.cpp ;

compile ref_ct_test.cpp ;
run ref_test.cpp ;
compile-fail ref_rv_fail1.cpp ;
compile-fail ref_rv_fail2.cpp ;
compile-fail ref_rv_fail3.cpp ;

run eif_constructors.cpp ;
run eif_dummy_arg_disambiguation.cpp ;
Expand Down
21 changes: 21 additions & 0 deletions test/ref_rv_fail1.cpp
@@ -0,0 +1,21 @@
// Copyright 2002-2004 David Abrahams and Aleksey Gurtovoy
// Copyright 2014 Agustin Berge
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/ref.hpp>

# if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)

int main()
{
boost::reference_wrapper<int const> r(1); // this should produce an ERROR

return 0;
}

# else
# error To fail, this test requires rvalue references
# endif
15 changes: 15 additions & 0 deletions test/ref_rv_fail2.cpp
@@ -0,0 +1,15 @@
// Copyright 2002-2004 David Abrahams and Aleksey Gurtovoy
// Copyright 2014 Agustin Berge
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/ref.hpp>

int main()
{
boost::reference_wrapper<int> r = boost::ref(2); // this should produce an ERROR

return 0;
}
25 changes: 25 additions & 0 deletions test/ref_rv_fail3.cpp
@@ -0,0 +1,25 @@
// Copyright 2002-2004 David Abrahams and Aleksey Gurtovoy
// Copyright 2014 Agustin Berge
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/ref.hpp>

# if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)

struct X {};

X const crv() { return X(); }

int main()
{
boost::reference_wrapper<X const> r = boost::ref(crv()); // this should produce an ERROR

return 0;
}

# else
# error To fail, this test requires rvalue references
# endif

0 comments on commit 45f7564

Please sign in to comment.