fix doc example typo
[boost.git] / boost / ref.hpp
blob6058d6983197a72ab040bbfeb7e0b04006cc3ece
1 #ifndef BOOST_REF_HPP_INCLUDED
2 #define BOOST_REF_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
10 #include <boost/config.hpp>
11 #include <boost/utility/addressof.hpp>
12 #include <boost/mpl/bool.hpp>
13 #include <boost/detail/workaround.hpp>
16 // ref.hpp - ref/cref, useful helper functions
18 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
19 // Copyright (C) 2001, 2002 Peter Dimov
20 // Copyright (C) 2002 David Abrahams
22 // Distributed under the Boost Software License, Version 1.0. (See
23 // accompanying file LICENSE_1_0.txt or copy at
24 // http://www.boost.org/LICENSE_1_0.txt)
26 // See http://www.boost.org/libs/bind/ref.html for documentation.
29 namespace boost
32 template<class T> class reference_wrapper
34 public:
35 typedef T type;
37 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, < 1300 )
39 explicit reference_wrapper(T& t): t_(&t) {}
41 #else
43 explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}
45 #endif
47 operator T& () const { return *t_; }
49 T& get() const { return *t_; }
51 T* get_pointer() const { return t_; }
53 private:
55 T* t_;
58 # if defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581) )
59 # define BOOST_REF_CONST
60 # else
61 # define BOOST_REF_CONST const
62 # endif
64 template<class T> inline reference_wrapper<T> BOOST_REF_CONST ref(T & t)
66 return reference_wrapper<T>(t);
69 template<class T> inline reference_wrapper<T const> BOOST_REF_CONST cref(T const & t)
71 return reference_wrapper<T const>(t);
74 # undef BOOST_REF_CONST
76 # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
78 template<typename T>
79 class is_reference_wrapper
80 : public mpl::false_
84 template<typename T>
85 class unwrap_reference
87 public:
88 typedef T type;
91 # define AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(X) \
92 template<typename T> \
93 class is_reference_wrapper< X > \
94 : public mpl::true_ \
95 { \
96 }; \
98 template<typename T> \
99 class unwrap_reference< X > \
101 public: \
102 typedef T type; \
103 }; \
104 /**/
106 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T>)
107 #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
108 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T> const)
109 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T> volatile)
110 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T> const volatile)
111 #endif
113 # undef AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF
115 # else // no partial specialization
117 } // namespace boost
119 #include <boost/type.hpp>
121 namespace boost
124 namespace detail
126 typedef char (&yes_reference_wrapper_t)[1];
127 typedef char (&no_reference_wrapper_t)[2];
129 no_reference_wrapper_t is_reference_wrapper_test(...);
131 template<typename T>
132 yes_reference_wrapper_t is_reference_wrapper_test(type< reference_wrapper<T> >);
134 template<bool wrapped>
135 struct reference_unwrapper
137 template <class T>
138 struct apply
140 typedef T type;
144 template<>
145 struct reference_unwrapper<true>
147 template <class T>
148 struct apply
150 typedef typename T::type type;
155 template<typename T>
156 class is_reference_wrapper
158 public:
159 BOOST_STATIC_CONSTANT(
160 bool, value = (
161 sizeof(detail::is_reference_wrapper_test(type<T>()))
162 == sizeof(detail::yes_reference_wrapper_t)));
164 typedef ::boost::mpl::bool_<value> type;
167 template <typename T>
168 class unwrap_reference
169 : public detail::reference_unwrapper<
170 is_reference_wrapper<T>::value
171 >::template apply<T>
174 # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
176 template <class T> inline typename unwrap_reference<T>::type&
177 unwrap_ref(T& t)
179 return t;
182 template<class T> inline T* get_pointer( reference_wrapper<T> const & r )
184 return r.get_pointer();
187 } // namespace boost
189 #endif // #ifndef BOOST_REF_HPP_INCLUDED