Add implicit conversion between compatible reference wrappers (refs #83)

This commit is contained in:
Peter Dimov
2020-11-19 18:57:56 +02:00
parent 7bc2873e38
commit 54671134ae
5 changed files with 121 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <boost/core/addressof.hpp>
#include <boost/core/enable_if.hpp>
//
// ref.hpp - ref/cref, useful helper functions
@@ -46,6 +47,26 @@ namespace boost
#endif
namespace detail
{
template< class Y, class T > struct ref_convertible
{
typedef char (&yes) [1];
typedef char (&no) [2];
static yes f( T* );
static no f( ... );
enum _vt { value = sizeof( (f)( static_cast<Y*>(0) ) ) == sizeof(yes) };
};
struct ref_empty
{
};
} // namespace detail
// reference_wrapper
/**
@@ -87,6 +108,14 @@ public:
public:
#endif
template<class Y> friend class reference_wrapper;
template<class Y> reference_wrapper( reference_wrapper<Y> r,
typename enable_if_c<boost::detail::ref_convertible<Y, T>::value,
boost::detail::ref_empty>::type = boost::detail::ref_empty() ): t_( r.t_ )
{
}
/**
@return The stored reference.
@remark Does not throw.