• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[/
2  Copyright 2014 Peter Dimov
3
4  Distributed under the Boost Software License, Version 1.0.
5
6  See accompanying file LICENSE_1_0.txt
7  or copy at http://boost.org/LICENSE_1_0.txt
8]
9
10[section:addressof addressof]
11
12[simplesect Authors]
13
14* Brad King
15* Douglas Gregor
16* Peter Dimov
17* Glen Fernandes
18
19[endsimplesect]
20
21[section Header <boost/core/addressof.hpp>]
22
23The header `<boost/core/addressof.hpp>` defines the function
24template `boost::addressof`. `boost::addressof(x)` returns the
25address of `x`. Ordinarily, this address can be obtained by
26`&x`, but the unary `&` operator can be overloaded. `boost::addressof`
27avoids calling used-defined `operator&()`.
28
29`boost::addressof` was originally contributed by Brad King
30based on ideas from discussion with Doug Gregor.
31
32[section Synopsis]
33
34``
35namespace boost
36{
37    template<class T> T* addressof( T& x );
38}
39``
40
41[endsect]
42
43[section Example]
44
45``
46#include <boost/core/addressof.hpp>
47
48struct useless_type { };
49
50class nonaddressable {
51    useless_type operator&() const;
52};
53
54void f() {
55    nonaddressable x;
56    nonaddressable* xp = boost::addressof(x);
57    // nonaddressable* xpe = &x; /* error */
58}
59``
60
61[endsect]
62
63[section Notes]
64
65In C++11 and above, `boost::addressof` is conditionally
66`constexpr` when possible. This is indicated by
67`BOOST_CORE_NO_CONSTEXPR_ADDRESSOF` not being defined.
68
69With supported compilers, `boost::addressof` is always
70`constexpr` by leveraging compiler intrinsics. This is
71indicated by `BOOST_CORE_HAS_BUILTIN_ADDRESSOF` being
72defined.
73
74[endsect]
75
76[endsect]
77
78[endsect]
79