1 // Copyright (C) 2002 Brad King (brad.king@kitware.com)
2 // Douglas Gregor (gregod@cs.rpi.edu)
4 // Copyright (C) 2002, 2008 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_UTILITY_ADDRESSOF_HPP
13 # define BOOST_UTILITY_ADDRESSOF_HPP
15 # include <boost/config.hpp>
16 # include <boost/detail/workaround.hpp>
24 template<class T> struct addr_impl_ref
28 inline addr_impl_ref( T & v ): v_( v ) {}
29 inline operator T& () const { return v_; }
32 addr_impl_ref & operator=(const addr_impl_ref &);
35 template<class T> struct addressof_impl
37 static inline T * f( T & v, long )
39 return reinterpret_cast<T*>(
40 &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
43 static inline T * f( T * v, int )
51 template<class T> T * addressof( T & v )
53 #if defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x610 ) )
55 return boost::detail::addressof_impl<T>::f( v, 0 );
59 return boost::detail::addressof_impl<T>::f( boost::detail::addr_impl_ref<T>( v ), 0 );
64 #if defined( __SUNPRO_CC ) && BOOST_WORKAROUND( __SUNPRO_CC, BOOST_TESTED_AT( 0x590 ) )
69 template<class T> struct addressof_addp
76 template< class T, std::size_t N >
77 typename detail::addressof_addp< T[N] >::type addressof( T (&t)[N] )
84 // Borland doesn't like casting an array reference to a char reference
85 // but these overloads work around the problem.
86 #if defined( __BORLANDC__ ) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
87 template<typename T,std::size_t N>
88 T (*addressof(T (&t)[N]))[N]
90 return reinterpret_cast<T(*)[N]>(&t);
93 template<typename T,std::size_t N>
94 const T (*addressof(const T (&t)[N]))[N]
96 return reinterpret_cast<const T(*)[N]>(&t);
102 #endif // BOOST_UTILITY_ADDRESSOF_HPP