addressof.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2002 Brad King (brad.king@kitware.com)
  2. // Douglas Gregor (gregod@cs.rpi.edu)
  3. //
  4. // Copyright (C) 2002, 2008 Peter Dimov
  5. //
  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)
  9. // For more information, see http://www.boost.org
  10. #ifndef BOOST_UTILITY_ADDRESSOF_HPP
  11. # define BOOST_UTILITY_ADDRESSOF_HPP
  12. # include <boost/config.hpp>
  13. # include <boost/detail/workaround.hpp>
  14. namespace boost
  15. {
  16. namespace detail
  17. {
  18. template<class T> struct addr_impl_ref
  19. {
  20. T & v_;
  21. inline addr_impl_ref( T & v ): v_( v ) {}
  22. inline operator T& () const { return v_; }
  23. private:
  24. addr_impl_ref & operator=(const addr_impl_ref &);
  25. };
  26. template<class T> struct addressof_impl
  27. {
  28. static inline T * f( T & v, long )
  29. {
  30. return reinterpret_cast<T*>(
  31. &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
  32. }
  33. static inline T * f( T * v, int )
  34. {
  35. return v;
  36. }
  37. };
  38. } // namespace detail
  39. template<class T> T * addressof( T & v )
  40. {
  41. #if defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x610 ) )
  42. return boost::detail::addressof_impl<T>::f( v, 0 );
  43. #else
  44. return boost::detail::addressof_impl<T>::f( boost::detail::addr_impl_ref<T>( v ), 0 );
  45. #endif
  46. }
  47. #if defined( __SUNPRO_CC ) && BOOST_WORKAROUND( __SUNPRO_CC, BOOST_TESTED_AT( 0x590 ) )
  48. namespace detail
  49. {
  50. template<class T> struct addressof_addp
  51. {
  52. typedef T * type;
  53. };
  54. } // namespace detail
  55. template< class T, std::size_t N >
  56. typename detail::addressof_addp< T[N] >::type addressof( T (&t)[N] )
  57. {
  58. return &t;
  59. }
  60. #endif
  61. // Borland doesn't like casting an array reference to a char reference
  62. // but these overloads work around the problem.
  63. #if defined( __BORLANDC__ ) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  64. template<typename T,std::size_t N>
  65. T (*addressof(T (&t)[N]))[N]
  66. {
  67. return reinterpret_cast<T(*)[N]>(&t);
  68. }
  69. template<typename T,std::size_t N>
  70. const T (*addressof(const T (&t)[N]))[N]
  71. {
  72. return reinterpret_cast<const T(*)[N]>(&t);
  73. }
  74. #endif
  75. } // namespace boost
  76. #endif // BOOST_UTILITY_ADDRESSOF_HPP