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:default_allocator default_allocator] 10 11[simplesect Authors] 12 13* Glen Fernandes 14 15[endsimplesect] 16 17[section Overview] 18 19The header <boost/core/default_allocator.hpp> provides the class template 20`boost::default_allocator` to serve as a minimal default allocator that: 21 22* Like C++2a's `std::allocator`, does not provide members such as `construct()` 23and `destroy()` to be eligible for optimizations by allocator-aware code that 24detects the absence of these members to provide more optimal construction. 25* Supports `BOOST_NO_EXCEPTIONS` in allocation. 26* Does not have `std` as an associated namespace. 27 28[endsect] 29 30[section Examples] 31 32The following snippet shows the use of this allocator as the default allocator 33for a container. 34 35``` 36template<class Key, class Compare = std::less<Key>, 37 class Allocator = boost::default_allocator<Key> > 38class FlatSet; 39``` 40 41Facilities like `make_shared` can be implemented using `allocate_shared` with 42`default_allocator`. 43 44``` 45template<class T, class... Args> 46enable_if_t<!is_array_v<T>, shared_ptr<T> > 47make_shared(Args&&... args) 48{ 49 return allocate_shared<T>(boost::default_allocator<remove_cv_t<T> >(), 50 std::forward<Args>(args)...); 51} 52``` 53 54[endsect] 55 56[section Reference] 57 58``` 59namespace boost { 60 61template<class T> 62struct default_allocator { 63 typedef T value_type; 64 typedef T* pointer; 65 typedef const T* const_pointer; 66 typedef std::add_lvalue_reference_t<T> reference; 67 typedef std::add_lvalue_reference_t<const T> const_reference; 68 typedef std::size_t size_type; 69 typedef std::ptrdiff_t difference_type; 70 typedef ``['true_type]`` propagate_on_container_move_assignment; 71 typedef ``['true_type]`` is_always_equal; 72 73 template<class U> 74 struct rebind { 75 typedef default_allocator<U> other; 76 }; 77 78 constexpr default_allocator() = default; 79 80 template<class U> 81 constexpr default_allocator(const default_allocator<U>&) noexcept { } 82 83 constexpr std::size_t max_size() const noexcept; 84 T* allocate(std::size_t n); 85 void deallocate(T* p, std::size_t); 86}; 87 88template<class T, class U> 89constexpr bool operator==(const default_allocator<T>&, 90 const default_allocator<U>&) noexcept; 91 92template<class T, class U> 93constexpr bool operator!=(const default_allocator<T>&, 94 const default_allocator<U>&) noexcept; 95 96} /* boost */ 97``` 98 99[section Members] 100 101[variablelist 102[[`constexpr std::size_t max_size() const noexcept;`] 103[[variablelist 104[[Returns][The largest value `N` for which the call `allocate(N)` might 105succeed.]]]]] 106[[`T* allocate(std::size_t n);`] 107[[variablelist 108[[Returns] 109[A pointer to the initial element of an array of storage of size 110`n * sizeof(T)`, aligned appropriately for objects of type `T`.]] 111[[Remarks][The storage is obtained by calling `::operator new`.]] 112[[Throws][`std::bad_alloc` if the storage cannot be obtained.]]]]] 113[[`void deallocate(T* p, std::size_t n);`] 114[[variablelist 115[[Requires] 116[`p` shall be a pointer value obtained from `allocate()`. `n` shall equal the 117value passed as the first argument to the invocation of `allocate` which 118returned `p`.]] 119[[Effects][Deallocates the storage referenced by `p`.]] 120[[Remarks][Uses `::operator delete`.]]]]]] 121 122[endsect] 123 124[section Operators] 125 126[variablelist 127[[`template<class T, class U> constexpr bool operator==(const 128default_allocator<T>&, const default_allocator<U>&) noexcept;`] 129[[variablelist 130[[Returns][`true`.]]]]] 131[[`template<class T, class U> constexpr bool operator!=(const 132default_allocator<T>&, const default_allocator<U>&) noexcept;`] 133[[variablelist 134[[Returns][`false`.]]]]]] 135 136[endsect] 137 138[endsect] 139 140[endsect] 141