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/utility/addressof.hpp>
12 #include <boost/detail/workaround.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 // glenfe at live dot com
23 // Copyright (C) 2014 Agustin Berge
25 // Distributed under the Boost Software License, Version 1.0. (See
26 // accompanying file LICENSE_1_0.txt or copy at
27 // http://www.boost.org/LICENSE_1_0.txt)
29 // See http://www.boost.org/libs/core/doc/html/core/ref.html for documentation.
42 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
44 struct ref_workaround_tag {};
51 @brief Contains a reference to an object of type `T`.
53 `reference_wrapper` is primarily used to "feed" references to
54 function templates (algorithms) that take their parameter by
55 value. It provides an implicit conversion to `T&`, which
56 usually allows the function templates to work on references
59 template<class T> class reference_wrapper
68 Constructs a `reference_wrapper` object that stores a
71 @remark Does not throw.
73 BOOST_FORCEINLINE explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}
75 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
77 BOOST_FORCEINLINE explicit reference_wrapper( T & t, ref_workaround_tag ): t_( boost::addressof( t ) ) {}
81 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
83 @remark Construction from a temporary object is disabled.
85 BOOST_DELETED_FUNCTION(reference_wrapper(T&& t))
90 @return The stored reference.
91 @remark Does not throw.
93 BOOST_FORCEINLINE operator T& () const { return *t_; }
96 @return The stored reference.
97 @remark Does not throw.
99 BOOST_FORCEINLINE T& get() const { return *t_; }
102 @return A pointer to the object referenced by the stored
104 @remark Does not throw.
106 BOOST_FORCEINLINE T* get_pointer() const { return t_; }
118 #if defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581) )
119 # define BOOST_REF_CONST
121 # define BOOST_REF_CONST const
128 @return `reference_wrapper<T>(t)`
129 @remark Does not throw.
131 template<class T> BOOST_FORCEINLINE reference_wrapper<T> BOOST_REF_CONST ref( T & t )
133 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
135 return reference_wrapper<T>( t, ref_workaround_tag() );
139 return reference_wrapper<T>( t );
147 @return `reference_wrapper<T const>(t)`
148 @remark Does not throw.
150 template<class T> BOOST_FORCEINLINE reference_wrapper<T const> BOOST_REF_CONST cref( T const & t )
152 return reference_wrapper<T const>(t);
155 #undef BOOST_REF_CONST
157 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
162 #if defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
163 # define BOOST_REF_DELETE
165 # define BOOST_REF_DELETE = delete
172 @remark Construction from a temporary object is disabled.
174 template<class T> void ref(T const&&) BOOST_REF_DELETE;
177 @remark Construction from a temporary object is disabled.
179 template<class T> void cref(T const&&) BOOST_REF_DELETE;
181 #undef BOOST_REF_DELETE
185 // is_reference_wrapper
188 @brief Determine if a type `T` is an instantiation of
191 The value static constant will be true if the type `T` is a
192 specialization of `reference_wrapper`.
194 template<typename T> struct is_reference_wrapper
196 BOOST_STATIC_CONSTANT( bool, value = false );
202 template<typename T> struct is_reference_wrapper< reference_wrapper<T> >
204 BOOST_STATIC_CONSTANT( bool, value = true );
207 #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
209 template<typename T> struct is_reference_wrapper< reference_wrapper<T> const >
211 BOOST_STATIC_CONSTANT( bool, value = true );
214 template<typename T> struct is_reference_wrapper< reference_wrapper<T> volatile >
216 BOOST_STATIC_CONSTANT( bool, value = true );
219 template<typename T> struct is_reference_wrapper< reference_wrapper<T> const volatile >
221 BOOST_STATIC_CONSTANT( bool, value = true );
224 #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS)
234 @brief Find the type in a `reference_wrapper`.
236 The `typedef` type is `T::type` if `T` is a
237 `reference_wrapper`, `T` otherwise.
239 template<typename T> struct unwrap_reference
247 template<typename T> struct unwrap_reference< reference_wrapper<T> >
252 #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
254 template<typename T> struct unwrap_reference< reference_wrapper<T> const >
259 template<typename T> struct unwrap_reference< reference_wrapper<T> volatile >
264 template<typename T> struct unwrap_reference< reference_wrapper<T> const volatile >
269 #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS)
278 @return `unwrap_reference<T>::type&(t)`
279 @remark Does not throw.
281 template<class T> BOOST_FORCEINLINE typename unwrap_reference<T>::type& unwrap_ref( T & t )
291 template<class T> BOOST_FORCEINLINE T* get_pointer( reference_wrapper<T> const & r )
293 return r.get_pointer();
301 #endif // #ifndef BOOST_CORE_REF_HPP