1 #ifndef BOOST_CORE_REF_HPP
2 #define BOOST_CORE_REF_HPP
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 #include <boost/config.hpp>
11 #include <boost/config/workaround.hpp>
12 #include <boost/core/addressof.hpp>
15 // ref.hpp - ref/cref, useful helper functions
17 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
18 // Copyright (C) 2001, 2002 Peter Dimov
19 // Copyright (C) 2002 David Abrahams
21 // Copyright (C) 2014 Glen Joseph Fernandes
22 // (glenjofe@gmail.com)
24 // Copyright (C) 2014 Agustin Berge
26 // Distributed under the Boost Software License, Version 1.0. (See
27 // accompanying file LICENSE_1_0.txt or copy at
28 // http://www.boost.org/LICENSE_1_0.txt)
30 // See http://www.boost.org/libs/core/doc/html/core/ref.html for documentation.
43 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
45 struct ref_workaround_tag {};
52 @brief Contains a reference to an object of type `T`.
54 `reference_wrapper` is primarily used to "feed" references to
55 function templates (algorithms) that take their parameter by
56 value. It provides an implicit conversion to `T&`, which
57 usually allows the function templates to work on references
60 template<class T> class reference_wrapper
69 Constructs a `reference_wrapper` object that stores a
72 @remark Does not throw.
74 BOOST_FORCEINLINE explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}
76 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
78 BOOST_FORCEINLINE explicit reference_wrapper( T & t, ref_workaround_tag ): t_( boost::addressof( t ) ) {}
82 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
84 @remark Construction from a temporary object is disabled.
86 BOOST_DELETED_FUNCTION(reference_wrapper(T&& t))
91 @return The stored reference.
92 @remark Does not throw.
94 BOOST_FORCEINLINE operator T& () const { return *t_; }
97 @return The stored reference.
98 @remark Does not throw.
100 BOOST_FORCEINLINE T& get() const { return *t_; }
103 @return A pointer to the object referenced by the stored
105 @remark Does not throw.
107 BOOST_FORCEINLINE T* get_pointer() const { return t_; }
119 #if defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581) )
120 # define BOOST_REF_CONST
122 # define BOOST_REF_CONST const
129 @return `reference_wrapper<T>(t)`
130 @remark Does not throw.
132 template<class T> BOOST_FORCEINLINE reference_wrapper<T> BOOST_REF_CONST ref( T & t )
134 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
136 return reference_wrapper<T>( t, ref_workaround_tag() );
140 return reference_wrapper<T>( t );
148 @return `reference_wrapper<T const>(t)`
149 @remark Does not throw.
151 template<class T> BOOST_FORCEINLINE reference_wrapper<T const> BOOST_REF_CONST cref( T const & t )
153 return reference_wrapper<T const>(t);
156 #undef BOOST_REF_CONST
158 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
163 #if defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
164 # define BOOST_REF_DELETE
166 # define BOOST_REF_DELETE = delete
173 @remark Construction from a temporary object is disabled.
175 template<class T> void ref(T const&&) BOOST_REF_DELETE;
178 @remark Construction from a temporary object is disabled.
180 template<class T> void cref(T const&&) BOOST_REF_DELETE;
182 #undef BOOST_REF_DELETE
186 // is_reference_wrapper
189 @brief Determine if a type `T` is an instantiation of
192 The value static constant will be true if the type `T` is a
193 specialization of `reference_wrapper`.
195 template<typename T> struct is_reference_wrapper
197 BOOST_STATIC_CONSTANT( bool, value = false );
203 template<typename T> struct is_reference_wrapper< reference_wrapper<T> >
205 BOOST_STATIC_CONSTANT( bool, value = true );
208 #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
210 template<typename T> struct is_reference_wrapper< reference_wrapper<T> const >
212 BOOST_STATIC_CONSTANT( bool, value = true );
215 template<typename T> struct is_reference_wrapper< reference_wrapper<T> volatile >
217 BOOST_STATIC_CONSTANT( bool, value = true );
220 template<typename T> struct is_reference_wrapper< reference_wrapper<T> const volatile >
222 BOOST_STATIC_CONSTANT( bool, value = true );
225 #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS)
235 @brief Find the type in a `reference_wrapper`.
237 The `typedef` type is `T::type` if `T` is a
238 `reference_wrapper`, `T` otherwise.
240 template<typename T> struct unwrap_reference
248 template<typename T> struct unwrap_reference< reference_wrapper<T> >
253 #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
255 template<typename T> struct unwrap_reference< reference_wrapper<T> const >
260 template<typename T> struct unwrap_reference< reference_wrapper<T> volatile >
265 template<typename T> struct unwrap_reference< reference_wrapper<T> const volatile >
270 #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS)
279 @return `unwrap_reference<T>::type&(t)`
280 @remark Does not throw.
282 template<class T> BOOST_FORCEINLINE typename unwrap_reference<T>::type& unwrap_ref( T & t )
292 template<class T> BOOST_FORCEINLINE T* get_pointer( reference_wrapper<T> const & r )
294 return r.get_pointer();
302 #endif // #ifndef BOOST_CORE_REF_HPP