Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. — Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

addressof
PrevUpHomeNext

Authors

  • Brad King
  • Douglas Gregor
  • Peter Dimov

The header <boost/core/addressof.hpp> defines the function template boost::addressof. boost::addressof(x) returns the address of x. Ordinarily, this address can be obtained by &x, but the unary & operator can be overloaded. boost::addressof avoids calling used-defined operator&().

boost::addressof was originally contributed by Brad King based on ideas from discussion with Doug Gregor.

namespace boost
{
    template<class T> T* addressof( T& x );
}
#include <boost/core/addressof.hpp>

struct useless_type { };

class nonaddressable {
    useless_type operator&() const;
};

void f() {
    nonaddressable x;
    nonaddressable* xp = boost::addressof(x);
    // nonaddressable* xpe = &x; /* error */
}

PrevUpHomeNext