[/ 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 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 #include #include int main() { std::vector > > 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 enable_if_t, shared_ptr > allocate_shared_noinit(const A& a, size_t n) { return allocate_shared(boost::noinit_adapt(a), n); } template enable_if_t, shared_ptr > allocate_shared_noinit(const A& a) { return allocate_shared(boost::noinit_adapt(a)); } ``` [endsect] [section Reference] ``` namespace boost { template struct noinit_adaptor : A { template struct rebind { typedef noinit_adaptor::template rebind_alloc > other; }; noinit_adaptor() noexcept; template noinit_adaptor(U&& u) noexcept; template noinit_adaptor(const noinit_adaptor& u) noexcept; template void construct(U* p); template void construct(U* p, V&& v, Args&&... args); template void destroy(U* p); }; template bool operator==(const noinit_adaptor& lhs, const noinit_adaptor& rhs) noexcept; template bool operator!=(const noinit_adaptor& lhs, const noinit_adaptor& rhs) noexcept; template noinit_adaptor noinit_adapt(const A& a) noexcept; } /* boost */ ``` [section Constructors] [variablelist [[`noinit_adaptor() noexcept;`] [[variablelist [[Effects][Value initializes the A base class.]]]]] [[`template noinit_adaptor(U&& u) noexcept;`] [[variablelist [[Requires][`A` shall be constructible from `U`.]] [[Effects][Initializes the `A` base class with `std::forward(u)`.]]]]] [[`template noinit_adaptor(const noinit_adaptor& u) noexcept;`] [[variablelist [[Requires][`A` shall be constructible from `U`.]] [[Effects][Initializes the `A` base class with `static_cast(u)`.]]]]]] [endsect] [section Member functions] [variablelist [[`template void construct(U* p);`] [[variablelist [[Effects][`::new((void*)p) U`.]]]]] [[`template void construct(U* p, V&& v, Args&&... args);`] [[variablelist [[Effects][`::new(void*)p) U(std::forward(v), std::forward(args)...)`.]]]]] [[`template void destroy(U* p);`] [[variablelist [[Effects][`p->~U()`.]]]]]] [endsect] [section Operators] [variablelist [[`template constexpr bool operator==(const noinit_adaptor& lhs, const noinit_adaptor& rhs) noexcept;`] [[variablelist [[Returns][`static_cast(lhs) == static_cast(rhs)`.]]]]] [[`template constexpr bool operator!=(const noinit_adaptor& lhs, const noinit_adaptor& rhs) noexcept;`] [[variablelist [[Returns][`!(lhs == rhs)`.]]]]]] [endsect] [section Free functions] [variablelist [[`template noinit_adaptor noinit_adapt(const A& a) noexcept;`] [[variablelist [[Returns][`noinit_adaptor(a)`.]]]]]] [endsect] [endsect] [endsect]