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