pointer_traits.qbk 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. [/
  2. Copyright 2017-2018 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:pointer_traits pointer_traits]
  8. [simplesect Authors]
  9. * Glen Fernandes
  10. [endsimplesect]
  11. [section Overview]
  12. The header <boost/core/pointer_traits.hpp> provides the class template
  13. `boost::pointer_traits` to facilitate use of pointer-like types. The C++11
  14. standard library introduced `std::pointer_traits` along with an allocator
  15. model which supported pointer-like types in addition to plain raw pointers.
  16. This implementation also supports C++98.
  17. It also provides the function template `boost::to_address` to obtain a raw
  18. pointer from an object of any pointer-like type.
  19. [endsect]
  20. [section Examples]
  21. The following example allocates storage and constructs an object in that
  22. storage using an allocator.
  23. ```
  24. template<class Allocator>
  25. void function(Allocator& a)
  26. {
  27. auto p = a.allocate(1);
  28. std::allocator_traits<Allocator>::construct(a, boost::to_address(p));
  29. }
  30. ```
  31. [endsect]
  32. [section Reference]
  33. ```
  34. namespace boost {
  35. template<class T> struct pointer_traits {
  36. typedef T pointer;
  37. typedef ``['see below]`` element_type;
  38. typedef ``['see below]`` difference_type;
  39. template<class U> struct rebind_to { typedef ``['see below]`` type; };
  40. template<class U> using rebind = typename rebind_to<U>::type;
  41. static pointer pointer_to(``['see below]`` v);
  42. };
  43. template<class T> struct pointer_traits<T*> {
  44. typedef T* pointer;
  45. typedef T element_type;
  46. typedef std::ptrdiff_t difference_type;
  47. template<class U> struct rebind_to { typedef U* type; };
  48. template<class U> using rebind = typename rebind_to<U>::type;
  49. static pointer pointer_to(``['see below]`` v) noexcept;
  50. };
  51. template<class T>
  52. constexpr T* to_address(T* v) noexcept;
  53. template<class T>
  54. auto to_address(const T& v) noexcept;
  55. }
  56. ```
  57. [section Member types]
  58. [variablelist
  59. [[`typedef` ['see below] `element_type;`]
  60. [`T::element_type` if such a type exists; otherwise `U` if `T` is a class
  61. template instantiation of the form `Pointer<U, Args>`, where `Args` is zero
  62. or more type arguments; otherwise the specialization is ill-formed.]]
  63. [[`typedef` ['see below] `difference_type;`]
  64. [`T::difference_type` if such a type exists; otherwise `std::ptrdiff_t`.]]
  65. [[`template<class U> struct rebind_to { typedef` ['see below] `type; };`]
  66. [`type` is `T::rebind<U>` if such a type exists; otherwise, `Pointer<V, Args>`
  67. if `T` is a class template instantiation of the form `Pointer<T, Args>`,
  68. where `Args` is zero or more type arguments; otherwise, the instantiation of
  69. `rebind_to` is ill-formed.]]]
  70. [endsect]
  71. [section Member functions]
  72. [variablelist
  73. [[`static pointer pointer_traits::pointer_to(`['see below] `v);`]
  74. [[variablelist
  75. [[Remark]
  76. [If `element_type` is a void type, the type of `v` is unspecified; otherwise,
  77. it is `element_type&`.]]
  78. [[Returns]
  79. [A pointer to `v` obtained by calling `T::pointer_to(v)`.]]]]]
  80. [[`static pointer pointer_traits<T*>::pointer_to(`['see below] `v) noexcept;`]
  81. [[variablelist
  82. [[Remark]
  83. [If `element_type` is a void type, the type of `v` is unspecified; otherwise,
  84. it is `element_type&`.]]
  85. [[Returns][`addressof(v)`.]]]]]]
  86. [endsect]
  87. [section Optional members]
  88. [variablelist
  89. [[`static element_type* to_address(pointer v) noexcept;`]
  90. [[variablelist
  91. [[Returns]
  92. [A pointer of type `element_type*` that references the same location as the
  93. argument `p`.]]
  94. [[Note]
  95. [This function should be the inverse of `pointer_to`. If defined, it
  96. customizes the behavior of the non-member function `to_address`.]]]]]]
  97. [endsect]
  98. [section Free functions]
  99. [variablelist
  100. [[`template<class T> constexpr T* to_address(T* v) noexcept;`]
  101. [[variablelist
  102. [[Returns][`v`.]]]]]
  103. [[`template<class T> auto to_address(const T& v) noexcept;`]
  104. [[variablelist
  105. [[Returns][`pointer_traits<T>::to_address(v)` if that
  106. expression is well-formed, otherwise `to_address(v.operator->())`.]]]]]]
  107. [endsect]
  108. [endsect]
  109. [section Acknowledgments]
  110. Glen Fernandes implemented `pointer_traits` and `to_address` with reviews and
  111. guidance from Peter Dimov.
  112. [endsect]
  113. [endsect]