noinit_adaptor.qbk 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. [/
  2. Copyright 2019 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:noinit_adaptor noinit_adaptor]
  8. [simplesect Authors]
  9. * Glen Fernandes
  10. [endsimplesect]
  11. [section Overview]
  12. The header <boost/core/noinit_adaptor.hpp> provides the class
  13. template `boost::noinit_adaptor` that converts any allocator into
  14. one whose `construct(ptr)` performs default initialization via placement new,
  15. and whose `destroy(ptr)` invokes `value_type` destructor directly.
  16. [endsect]
  17. [section Examples]
  18. The following example shows use of this allocator adaptor to achieve default
  19. initialization of elements of a trivial type, which are later assigned values.
  20. ```
  21. #include <boost/core/noinit_adaptor.hpp>
  22. #include <numeric>
  23. #include <vector>
  24. int main()
  25. {
  26. std::vector<int, boost::noinit_adaptor<std::allocator<int> > > v(5);
  27. std::iota(v.begin(), v.end(), 1);
  28. }
  29. ```
  30. The `allocate_shared_noinit` function templates are now implemented simply
  31. using `allocate_shared` with `noinit_adaptor`.
  32. ```
  33. template<class T, class A>
  34. enable_if_t<is_unbounded_array_v<T>, shared_ptr<T> >
  35. allocate_shared_noinit(const A& a, size_t n)
  36. {
  37. return allocate_shared<T>(boost::noinit_adapt(a), n);
  38. }
  39. template<class T, class A>
  40. enable_if_t<!is_unbounded_array_v<T>, shared_ptr<T> >
  41. allocate_shared_noinit(const A& a)
  42. {
  43. return allocate_shared<T>(boost::noinit_adapt(a));
  44. }
  45. ```
  46. [endsect]
  47. [section Reference]
  48. ```
  49. namespace boost {
  50. template<class A>
  51. struct noinit_adaptor
  52. : A {
  53. template<class U>
  54. struct rebind {
  55. typedef noinit_adaptor<typename std::allocator_traits<A>::template
  56. rebind_alloc<U> > other;
  57. };
  58. noinit_adaptor() noexcept;
  59. template<class U>
  60. noinit_adaptor(U&& u) noexcept;
  61. template<class U>
  62. noinit_adaptor(const noinit_adaptor<U>& u) noexcept;
  63. template<class U>
  64. void construct(U* p);
  65. template<class U, class V, class... Args>
  66. void construct(U* p, V&& v, Args&&... args);
  67. template<class U>
  68. void destroy(U* p);
  69. };
  70. template<class T, class U>
  71. bool operator==(const noinit_adaptor<T>& lhs,
  72. const noinit_adaptor<U>& rhs) noexcept;
  73. template<class T, class U>
  74. bool operator!=(const noinit_adaptor<T>& lhs,
  75. const noinit_adaptor<U>& rhs) noexcept;
  76. template<class A>
  77. noinit_adaptor<A> noinit_adapt(const A& a) noexcept;
  78. } /* boost */
  79. ```
  80. [section Constructors]
  81. [variablelist
  82. [[`noinit_adaptor() noexcept;`]
  83. [[variablelist
  84. [[Effects][Value initializes the A base class.]]]]]
  85. [[`template<class U> noinit_adaptor(U&& u) noexcept;`]
  86. [[variablelist
  87. [[Requires][`A` shall be constructible from `U`.]]
  88. [[Effects][Initializes the `A` base class with `std::forward<U>(u)`.]]]]]
  89. [[`template<class U> noinit_adaptor(const noinit_adaptor<U>& u) noexcept;`]
  90. [[variablelist
  91. [[Requires][`A` shall be constructible from `U`.]]
  92. [[Effects][Initializes the `A` base class with
  93. `static_cast<const U&>(u)`.]]]]]]
  94. [endsect]
  95. [section Member functions]
  96. [variablelist
  97. [[`template<class U> void construct(U* p);`]
  98. [[variablelist
  99. [[Effects][`::new((void*)p) U`.]]]]]
  100. [[`template<class U, class V, class... Args> void construct(U* p, V&& v,
  101. Args&&... args);`]
  102. [[variablelist
  103. [[Effects][`::new(void*)p) U(std::forward<V>(v),
  104. std::forward<Args>(args)...)`.]]]]]
  105. [[`template<class U> void destroy(U* p);`]
  106. [[variablelist
  107. [[Effects][`p->~U()`.]]]]]]
  108. [endsect]
  109. [section Operators]
  110. [variablelist
  111. [[`template<class T, class U> constexpr bool
  112. operator==(const noinit_adaptor<T>& lhs,
  113. const noinit_adaptor<U>& rhs) noexcept;`]
  114. [[variablelist
  115. [[Returns][`static_cast<const T&>(lhs) == static_cast<const U&>(rhs)`.]]]]]
  116. [[`template<class T, class U> constexpr bool
  117. operator!=(const noinit_adaptor<T>& lhs,
  118. const noinit_adaptor<U>& rhs) noexcept;`]
  119. [[variablelist
  120. [[Returns][`!(lhs == rhs)`.]]]]]]
  121. [endsect]
  122. [section Free functions]
  123. [variablelist
  124. [[`template<class A> noinit_adaptor<A> noinit_adapt(const A& a) noexcept;`]
  125. [[variablelist
  126. [[Returns][`noinit_adaptor<A>(a)`.]]]]]]
  127. [endsect]
  128. [endsect]
  129. [endsect]