CRLF
[ghsmtp.git] / default_init_allocator.hpp
blob1de6ca76aa5025a792ad667116da0b9ccb6487f7
1 #ifndef DEFAULT_INIT_ALLOCATOR_DOT_HPP
2 #define DEFAULT_INIT_ALLOCATOR_DOT_HPP
4 // Allocator adaptor that interposes construct() calls to convert
5 // value initialization into default initialization.
7 // <https://en.cppreference.com/w/cpp/container/vector/resize>
8 // <https://stackoverflow.com/a/21028912/273767>
10 #include <memory>
12 template <typename T, typename A = std::allocator<T>>
13 class default_init_allocator : public A {
14 typedef std::allocator_traits<A> a_t;
16 public:
17 template <typename U>
18 struct rebind {
19 using other
20 = default_init_allocator<U, typename a_t::template rebind_alloc<U>>;
23 using A::A;
25 template <typename U>
26 void
27 construct(U* ptr) noexcept(std::is_nothrow_default_constructible<U>::value)
29 ::new (static_cast<void*>(ptr)) U;
31 template <typename U, typename... Args>
32 void construct(U* ptr, Args&&... args)
34 a_t::construct(static_cast<A&>(*this), ptr, std::forward<Args>(args)...);
38 #endif // DEFAULT_INIT_ALLOCATOR_DOT_HPP