1 // Copyright (C) 2002 Brad King (brad.king@kitware.com)
2 // Douglas Gregor (gregod@cs.rpi.edu)
4 // Copyright (C) 2002, 2008, 2013 Peter Dimov
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see http://www.boost.org
12 #ifndef BOOST_CORE_ADDRESSOF_HPP
13 #define BOOST_CORE_ADDRESSOF_HPP
15 # include <boost/config.hpp>
16 # include <boost/detail/workaround.hpp>
25 template<class T> struct addr_impl_ref
29 BOOST_FORCEINLINE addr_impl_ref( T & v ): v_( v ) {}
30 BOOST_FORCEINLINE operator T& () const { return v_; }
33 addr_impl_ref & operator=(const addr_impl_ref &);
36 template<class T> struct addressof_impl
38 static BOOST_FORCEINLINE T * f( T & v, long )
40 return reinterpret_cast<T*>(
41 &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
44 static BOOST_FORCEINLINE T * f( T * v, int )
50 #if !defined( BOOST_NO_CXX11_NULLPTR )
52 #if !defined( BOOST_NO_CXX11_DECLTYPE ) && ( ( defined( __clang__ ) && !defined( _LIBCPP_VERSION ) ) || defined( __INTEL_COMPILER ) )
54 typedef decltype(nullptr) addr_nullptr_t;
58 typedef std::nullptr_t addr_nullptr_t;
62 template<> struct addressof_impl< addr_nullptr_t >
64 typedef addr_nullptr_t T;
66 static BOOST_FORCEINLINE T * f( T & v, int )
72 template<> struct addressof_impl< addr_nullptr_t const >
74 typedef addr_nullptr_t const T;
76 static BOOST_FORCEINLINE T * f( T & v, int )
82 template<> struct addressof_impl< addr_nullptr_t volatile >
84 typedef addr_nullptr_t volatile T;
86 static BOOST_FORCEINLINE T * f( T & v, int )
92 template<> struct addressof_impl< addr_nullptr_t const volatile >
94 typedef addr_nullptr_t const volatile T;
96 static BOOST_FORCEINLINE T * f( T & v, int )
104 } // namespace detail
108 T * addressof( T & v )
110 #if (defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x610 ) ) ) || (defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5120))
112 return boost::detail::addressof_impl<T>::f( v, 0 );
116 return boost::detail::addressof_impl<T>::f( boost::detail::addr_impl_ref<T>( v ), 0 );
121 #if defined( __SUNPRO_CC ) && BOOST_WORKAROUND( __SUNPRO_CC, BOOST_TESTED_AT( 0x590 ) )
126 template<class T> struct addressof_addp
131 } // namespace detail
133 template< class T, std::size_t N >
135 typename detail::addressof_addp< T[N] >::type addressof( T (&t)[N] )
142 // Borland doesn't like casting an array reference to a char reference
143 // but these overloads work around the problem.
144 #if defined( __BORLANDC__ ) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
145 template<typename T,std::size_t N>
147 T (*addressof(T (&t)[N]))[N]
149 return reinterpret_cast<T(*)[N]>(&t);
152 template<typename T,std::size_t N>
154 const T (*addressof(const T (&t)[N]))[N]
156 return reinterpret_cast<const T(*)[N]>(&t);
162 #endif // BOOST_CORE_ADDRESSOF_HPP