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