addressof.qbk 1.5 KB

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