Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-04-10 06:12:44

From: "Douglas Gregor" <gregod_at_[hidden]>
> 'tis done. addressof() has been added as boost/utility/addressof.hpp (also
> included in boost/utility.hpp), along with documentation and some
> I've also changed reference_wrapper to use addressof instead of the '&'
> operator.

template <typename T>
inline T* addressof(T& v)
  return reinterpret_cast<T*>(

template <typename T>
inline const T* addressof(const T& v)
  return reinterpret_cast<const T*>(
           &reinterpret_cast<const detail::addressof_helper&>(v));

This is the "famous" overloading that compiles 50% of the time; and when it
compiles, you are probably going to get the wrong overload; and T const &
allows you to apply addressof() to rvalues. ;-)

A version that doesn't have these problems:

template <class T> inline T * addressof(T & v)
  return reinterpret_cast<T*>(
    &const_cast<char &>(reinterpret_cast<char const &>(v)));

(replace char with detail::addressof_helper if you like; I'm not sure which
is better. I like the char& cast better when T is not a POD.)

C-style casts are much more readable. ;-)

Boost list run by bdawes at, gregod at, cpdaniel at, john at