| Home | Libraries | People | FAQ | More

## noinit_adaptor

### Authors

- Glen Fernandes

### 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.

### 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));
    }

### 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 */

#### Constructors

`noinit_adaptor() noexcept;`

  Effects

    Value initializes the A base class.

`template<class U> noinit_adaptor(U&& u) noexcept;`

  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;`

  Requires

    `A` shall be constructible from `U`.

  Effects

    Initializes the `A` base class with `static_cast<const A&>(u)`.

#### Member functions

`template<class U> void construct(U* p);`

  Effects

    `::new((void*)p) U`.

`template<class U> void destroy(U* p);`

  Effects

    `p->~U()`.

#### Operators

`template<class T, class U> constexpr bool operator==(const noinit_adaptor<T>& lhs, const noinit_adaptor<U>& rhs) noexcept;`

  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;`

  Returns

    `!(lhs == rhs)`.

#### Free functions

`template<class A> noinit_adaptor<A> noinit_adapt(const A& a) noexcept;`

  Returns

    `noinit_adaptor<A>(a)`.