From 68b80e6ac75a376a9ea52215b1ec0449f075d640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Wed, 17 Dec 2025 22:00:52 +0100 Subject: [PATCH] Implemented C++20's uninitialized_construct_using_allocator and make_obj_using_allocator. --- doc/container.qbk | 3 +- .../detail/dispatch_uses_allocator.hpp | 163 +++++++++++------- .../container/uses_allocator_construction.hpp | 24 +++ test/uses_allocator_construction_test.cpp | 139 +++++++++++---- 4 files changed, 237 insertions(+), 92 deletions(-) diff --git a/doc/container.qbk b/doc/container.qbk index cf0773a..161393c 100644 --- a/doc/container.qbk +++ b/doc/container.qbk @@ -1438,7 +1438,8 @@ use [*Boost.Container]? There are several reasons for that: * Implemented heterogeneous overloads from C++23 ([@http://wg21.link/p2077r3 P2077]) and C++26 ([@http://wg21.link/P2363 P2363]). * In C++20 compilers, `static_vector`'s destructor is now trivial if `T` is trivial. -* Implemented C++20's [classref boost::container::uninitialized_construct_using_allocator uninitialized_construct_using_allocator]. +* Implemented C++20's [funcref boost::container::uninitialized_construct_using_allocator uninitialized_construct_using_allocator] + and [funcref boost::container::make_obj_using_allocator make_obj_using_allocator]. * Fixed bugs/issues: * [@https://github.com/boostorg/container/issues/323 GitHub #323: ['"flat_tree::try_emplace UB"]]. diff --git a/include/boost/container/detail/dispatch_uses_allocator.hpp b/include/boost/container/detail/dispatch_uses_allocator.hpp index f88d233..0bce8c8 100644 --- a/include/boost/container/detail/dispatch_uses_allocator.hpp +++ b/include/boost/container/detail/dispatch_uses_allocator.hpp @@ -92,16 +92,16 @@ namespace dtl { // ///////////////////////////////////////////////////////////////////////// template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , typename T , class ...Args > inline typename dtl::enable_if_and < void , dtl::is_not_pair - , dtl::not_< uses_allocator::type > > + , dtl::not_< uses_allocator::type > > >::type dispatch_uses_allocator - ( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc), T* p, BOOST_FWD_REF(Args)...args) + ( ConstructAlloc & construct_alloc, BOOST_FWD_REF(AllocArg), T* p, BOOST_FWD_REF(Args)...args) { allocator_traits::construct(construct_alloc, p, ::boost::forward(args)...); } @@ -112,21 +112,21 @@ inline typename dtl::enable_if_and // ///////////////////////////////////////////////////////////////////////// template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , typename T , class ...Args > inline typename dtl::enable_if_and < void , dtl::is_not_pair - , uses_allocator::type> - , is_constructible_with_allocator_prefix + , uses_allocator::type> + , is_constructible_with_allocator_prefix >::type dispatch_uses_allocator - ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p, BOOST_FWD_REF(Args) ...args) + ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, T* p, BOOST_FWD_REF(Args) ...args) { allocator_traits::construct ( construct_alloc, p, allocator_arg - , ::boost::forward(arg_alloc), ::boost::forward(args)...); + , ::boost::forward(arg_alloc), ::boost::forward(args)...); } ///////////////////////////////////////////////////////////////////////// @@ -135,22 +135,22 @@ inline typename dtl::enable_if_and // ///////////////////////////////////////////////////////////////////////// template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , typename T , class ...Args > inline typename dtl::enable_if_and < void , dtl::is_not_pair - , uses_allocator::type> - , dtl::not_ > - , is_constructible_with_allocator_suffix + , uses_allocator::type> + , dtl::not_ > + , is_constructible_with_allocator_suffix >::type dispatch_uses_allocator - ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p, BOOST_FWD_REF(Args)...args) + ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, T* p, BOOST_FWD_REF(Args)...args) { allocator_traits::construct ( construct_alloc, p, ::boost::forward(args)... - , ::boost::forward(arg_alloc)); + , ::boost::forward(arg_alloc)); } ///////////////////////////////////////////////////////////////////////// @@ -159,18 +159,18 @@ inline typename dtl::enable_if_and // ///////////////////////////////////////////////////////////////////////// template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , typename T , class ...Args > inline typename dtl::enable_if_and < void , dtl::is_not_pair - , uses_allocator::type> - , dtl::not_ > - , dtl::not_ > + , uses_allocator::type> + , dtl::not_ > + , dtl::not_ > >::type dispatch_uses_allocator - ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc), T* p, BOOST_FWD_REF(Args)...args) + ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(AllocArg), T* p, BOOST_FWD_REF(Args)...args) { allocator_traits::construct(construct_alloc, p, ::boost::forward(args)...); } @@ -183,14 +183,14 @@ inline typename dtl::enable_if_and // ///////////////////////////////////////////////////////////////////////// #define BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE(N) \ - template \ + template \ inline typename dtl::enable_if_and\ < void\ , dtl::is_not_pair\ - , dtl::not_::type> >\ + , dtl::not_::type> >\ >::type\ dispatch_uses_allocator\ - (ConstructAlloc &construct_alloc, BOOST_FWD_REF(ArgAlloc), T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ + (ConstructAlloc &construct_alloc, BOOST_FWD_REF(AllocArg), T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ {\ allocator_traits::construct(construct_alloc, p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ }\ @@ -204,18 +204,18 @@ BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR // ///////////////////////////////////////////////////////////////////////// #define BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE(N) \ - template < typename ConstructAlloc, typename ArgAlloc, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\ + template < typename ConstructAlloc, typename AllocArg, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\ inline typename dtl::enable_if_and\ < void\ , dtl::is_not_pair\ - , uses_allocator::type>\ - , is_constructible_with_allocator_prefix\ + , uses_allocator::type>\ + , is_constructible_with_allocator_prefix\ >::type\ dispatch_uses_allocator\ - (ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ + (ConstructAlloc& construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ {\ allocator_traits::construct\ - (construct_alloc, p, allocator_arg, ::boost::forward(arg_alloc) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ + (construct_alloc, p, allocator_arg, ::boost::forward(arg_alloc) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ }\ // BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE) @@ -227,19 +227,19 @@ BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR // ///////////////////////////////////////////////////////////////////////// #define BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE(N) \ - template < typename ConstructAlloc, typename ArgAlloc, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\ + template < typename ConstructAlloc, typename AllocArg, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\ inline typename dtl::enable_if_and\ < void\ , dtl::is_not_pair\ - , uses_allocator::type>\ - , dtl::not_ >\ - , is_constructible_with_allocator_suffix\ + , uses_allocator::type>\ + , dtl::not_ >\ + , is_constructible_with_allocator_suffix\ >::type\ dispatch_uses_allocator\ - (ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ + (ConstructAlloc& construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ {\ allocator_traits::construct\ - (construct_alloc, p BOOST_MOVE_I##N BOOST_MOVE_FWD##N, ::boost::forward(arg_alloc));\ + (construct_alloc, p BOOST_MOVE_I##N BOOST_MOVE_FWD##N, ::boost::forward(arg_alloc));\ }\ // BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE) @@ -251,16 +251,16 @@ BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR // ///////////////////////////////////////////////////////////////////////// #define BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE(N) \ - template < typename ConstructAlloc, typename ArgAlloc, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\ + template < typename ConstructAlloc, typename AllocArg, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\ inline typename dtl::enable_if_and\ < void\ , dtl::is_not_pair\ - , uses_allocator::type>\ - , dtl::not_ >\ - , dtl::not_ >\ + , uses_allocator::type>\ + , dtl::not_ >\ + , dtl::not_ >\ >::type\ dispatch_uses_allocator\ - (ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc), T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ + (ConstructAlloc& construct_alloc, BOOST_FWD_REF(AllocArg), T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ {\ allocator_traits::construct(construct_alloc, p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ }\ @@ -271,13 +271,13 @@ BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR #endif //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , typename Pair > inline BOOST_CONTAINER_DOC1ST(void, typename dtl::enable_if BOOST_MOVE_I void >::type) dispatch_uses_allocator ( ConstructAlloc & construct_alloc - , BOOST_FWD_REF(ArgAlloc) arg_alloc + , BOOST_FWD_REF(AllocArg) arg_alloc , Pair* p) { dispatch_uses_allocator(construct_alloc, arg_alloc, dtl::addressof(p->first)); @@ -293,12 +293,12 @@ BOOST_CONTAINER_DOC1ST(void, typename dtl::enable_if BOOST_MO template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , class Pair, class U, class V> BOOST_CONTAINER_DOC1ST(void, typename dtl::enable_if BOOST_MOVE_I void>::type) dispatch_uses_allocator ( ConstructAlloc & construct_alloc - , BOOST_FWD_REF(ArgAlloc) arg_alloc + , BOOST_FWD_REF(AllocArg) arg_alloc , Pair* p, BOOST_FWD_REF(U) x, BOOST_FWD_REF(V) y) { dispatch_uses_allocator(construct_alloc, arg_alloc, dtl::addressof(p->first), ::boost::forward(x)); @@ -313,17 +313,17 @@ BOOST_CONTAINER_DOC1ST(void, typename dtl::enable_if BOOST_MO } template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , class Pair, class Pair2> BOOST_CONTAINER_DOC1ST(void, typename dtl::enable_if< dtl::is_pair BOOST_MOVE_I void >::type) dispatch_uses_allocator (ConstructAlloc & construct_alloc - , BOOST_FWD_REF(ArgAlloc) arg_alloc + , BOOST_FWD_REF(AllocArg) arg_alloc , Pair* p, Pair2& x) { dispatch_uses_allocator(construct_alloc, arg_alloc, p, x.first, x.second); } template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , class Pair, class Pair2> typename dtl::enable_if_and < void @@ -331,18 +331,18 @@ typename dtl::enable_if_and , dtl::not_ > >::type //This is needed for MSVC10 and ambiguous overloads dispatch_uses_allocator (ConstructAlloc & construct_alloc - , BOOST_FWD_REF(ArgAlloc) arg_alloc + , BOOST_FWD_REF(AllocArg) arg_alloc , Pair* p, BOOST_RV_REF_BEG Pair2 BOOST_RV_REF_END x) { dispatch_uses_allocator(construct_alloc, arg_alloc, p, ::boost::move(x.first), ::boost::move(x.second)); } //piecewise construction from boost::tuple #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BOOST_TUPLE_CODE(N,M)\ -template< typename ConstructAlloc, typename ArgAlloc, class Pair \ +template< typename ConstructAlloc, typename AllocArg, class Pair \ , template class BoostTuple \ BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \ typename dtl::enable_if< dtl::is_pair BOOST_MOVE_I void>::type\ - dispatch_uses_allocator( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t\ + dispatch_uses_allocator( ConstructAlloc & construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, Pair* pair, piecewise_construct_t\ , BoostTuple p\ , BoostTuple q)\ {\ @@ -366,9 +366,9 @@ BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BO //piecewise construction from Std Tuple #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) - template< typename ConstructAlloc, typename ArgAlloc, class Pair + template< typename ConstructAlloc, typename AllocArg, class Pair , template class Tuple, class... Args1, class... Args2, size_t... Indexes1, size_t... Indexes2> - void dispatch_uses_allocator_index( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair + void dispatch_uses_allocator_index( ConstructAlloc & construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, Pair* pair , Tuple& t1, Tuple& t2, index_tuple, index_tuple) { (void)t1; (void)t2; @@ -383,10 +383,10 @@ BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BO BOOST_CONTAINER_CATCH_END } - template< typename ConstructAlloc, typename ArgAlloc, class Pair + template< typename ConstructAlloc, typename AllocArg, class Pair , template class Tuple, class... Args1, class... Args2> typename dtl::enable_if< dtl::is_pair, void >::type - dispatch_uses_allocator( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t + dispatch_uses_allocator( ConstructAlloc & construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, Pair* pair, piecewise_construct_t , Tuple t1, Tuple t2) { (dispatch_uses_allocator_index)( construct_alloc, arg_alloc, pair, t1, t2 @@ -398,11 +398,11 @@ BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BO //MSVC 2010 tuple implementation #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE(N,M)\ - template< typename ConstructAlloc, typename ArgAlloc, class Pair\ + template< typename ConstructAlloc, typename AllocArg, class Pair\ , template class StdTuple\ BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \ typename dtl::enable_if< dtl::is_pair BOOST_MOVE_I void>::type\ - dispatch_uses_allocator(ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t\ + dispatch_uses_allocator(ConstructAlloc & construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, Pair* pair, piecewise_construct_t\ , StdTuple p\ , StdTuple q)\ {\ @@ -432,12 +432,12 @@ BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BO //MSVC 2012 tuple implementation #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_CODE(N,M)\ - template< typename ConstructAlloc, typename ArgAlloc, class Pair\ + template< typename ConstructAlloc, typename AllocArg, class Pair\ , template class StdTuple \ BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \ typename dtl::enable_if< dtl::is_pair BOOST_MOVE_I void>::type\ dispatch_uses_allocator\ - ( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t\ + ( ConstructAlloc & construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, Pair* pair, piecewise_construct_t\ , StdTuple p\ , StdTuple q)\ {\ @@ -461,14 +461,26 @@ BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BO #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +template +struct uses_allocator_destroy +{ + T &t_; + uses_allocator_destroy(T&t) + : t_(t) + {} + + ~uses_allocator_destroy() + { t_.~T(); } +}; + #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template < typename ConstructAlloc - , typename ArgAlloc + , typename AllocArg , class Pair, class KeyType, class ... Args> typename dtl::enable_if< dtl::is_pair, void >::type dispatch_uses_allocator - (ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* p, try_emplace_t, BOOST_FWD_REF(KeyType) k, BOOST_FWD_REF(Args) ...args) + (ConstructAlloc & construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, Pair* p, try_emplace_t, BOOST_FWD_REF(KeyType) k, BOOST_FWD_REF(Args) ...args) { dispatch_uses_allocator(construct_alloc, arg_alloc, dtl::addressof(p->first), ::boost::forward(k)); BOOST_CONTAINER_TRY{ @@ -481,14 +493,28 @@ typename dtl::enable_if< dtl::is_pair, void >::type BOOST_CONTAINER_CATCH_END } +template +inline T construct_dispatch_uses_allocator + (BOOST_FWD_REF(AllocArg) arg_alloc, BOOST_FWD_REF(Args)...args) +{ + typename dtl::aligned_storage::value>::type storage; + T * const addr = reinterpret_cast(&storage); + + boost::container::dtl::allocator_traits_dummy atd; + (dispatch_uses_allocator) + (atd, boost::forward(arg_alloc), addr, boost::forward(args)...); + uses_allocator_destroy d(*addr); + return d.t_; +} + #else #define BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_PAIR_TRY_EMPLACE_CODE(N) \ - template \ + template \ inline typename dtl::enable_if\ < dtl::is_pair BOOST_MOVE_I void >::type\ dispatch_uses_allocator\ - (ConstructAlloc &construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* p, try_emplace_t, \ + (ConstructAlloc &construct_alloc, BOOST_FWD_REF(AllocArg) arg_alloc, Pair* p, try_emplace_t, \ BOOST_FWD_REF(KeyType) k BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ {\ dispatch_uses_allocator(construct_alloc, arg_alloc, dtl::addressof(p->first), ::boost::forward(k));\ @@ -505,6 +531,23 @@ typename dtl::enable_if< dtl::is_pair, void >::type BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_PAIR_TRY_EMPLACE_CODE) #undef BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_PAIR_TRY_EMPLACE_CODE +#define BOOST_CONTAINER_CONSTRUCT_DISPATCH_USES_ALLOCATOR_CODE(N) \ + template \ + inline T construct_dispatch_uses_allocator\ + (BOOST_FWD_REF(AllocArg) arg_alloc BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ + {\ + typename dtl::aligned_storage::value>::type storage;\ + T * const addr = reinterpret_cast(&storage);\ + boost::container::dtl::allocator_traits_dummy atd;\ + (dispatch_uses_allocator)\ + (atd, boost::forward(arg_alloc), addr BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ + uses_allocator_destroy d(*addr);\ + return d.t_;\ + }\ +// +BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_CONSTRUCT_DISPATCH_USES_ALLOCATOR_CODE) +#undef BOOST_CONTAINER_CONSTRUCT_DISPATCH_USES_ALLOCATOR_CODE + #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) } //namespace dtl diff --git a/include/boost/container/uses_allocator_construction.hpp b/include/boost/container/uses_allocator_construction.hpp index 54e7bb6..6502232 100644 --- a/include/boost/container/uses_allocator_construction.hpp +++ b/include/boost/container/uses_allocator_construction.hpp @@ -51,6 +51,22 @@ T* uninitialized_construct_using_allocator(T* p, BOOST_FWD_REF(AllocArg) alloc_a return p; } +//! Effects: eates an object of the given type T by means of uses-allocator construction +//! (see `uses_allocator`), where: +//! +//! * `alloc_arg` is the allocator argument whose type AllocArg will be used to evaluate uses_allocator::value +//! * `args` are the arguments to pass to T's constructor. +//! +//! Returns: The newsly created object of type T +//! +//! Throws: Any exception thrown by the constructor of T. +template< class T, class AllocArg, class... Args > +T make_obj_using_allocator(BOOST_FWD_REF(AllocArg) alloc_arg, BOOST_FWD_REF(Args)... args) +{ + return boost::container::dtl::construct_dispatch_uses_allocator + (boost::forward(alloc_arg), boost::forward(args)...); +} + #else //BOOST_NO_CXX11_VARIADIC_TEMPLATES #define BOOST_CONTAINER_USES_ALLOCATOR_CONSTRUCTION_CODE(N) \ @@ -63,6 +79,14 @@ T* uninitialized_construct_using_allocator(T* p, BOOST_FWD_REF(AllocArg) alloc_a (atd, boost::forward(alloc_arg), p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ return p;\ }\ + \ + template \ + inline T make_obj_using_allocator\ + (BOOST_FWD_REF(AllocArg) alloc_arg BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ + {\ + return boost::container::dtl::construct_dispatch_uses_allocator\ + (boost::forward(alloc_arg) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ + }\ // BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_USES_ALLOCATOR_CONSTRUCTION_CODE) #undef BOOST_CONTAINER_USES_ALLOCATOR_CONSTRUCTION_CODE diff --git a/test/uses_allocator_construction_test.cpp b/test/uses_allocator_construction_test.cpp index bbb5475..9df5e61 100644 --- a/test/uses_allocator_construction_test.cpp +++ b/test/uses_allocator_construction_test.cpp @@ -90,7 +90,7 @@ struct uses_erased_type_allocator bool allocator_called; unsigned args_called; - typedef boost::container::erased_type allocator_type; + typedef erased_type allocator_type; typedef long constructible_from_int_t; uses_erased_type_allocator() @@ -118,32 +118,45 @@ struct uses_erased_type_allocator {} }; -typedef boost::container::dtl::aligned_storage::type storage_t; - -//Make sure aligned_storage will be big enough -BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_allocator_and_not_convertible_arg) ); -BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(not_uses_allocator) ); -BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_allocator_and_convertible_arg) ); -BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_erased_type_allocator) ); - -int main() +void test_uninitialized_construct() { + typedef dtl::aligned_storage::type storage_t; + + //Make sure aligned_storage will be big enough + BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_allocator_and_not_convertible_arg) ); + BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(not_uses_allocator) ); + BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_allocator_and_convertible_arg) ); + BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_erased_type_allocator) ); + storage_t storage; void *const st_ptr = static_cast(storage.data); - not_uses_allocator *nua_ptr = reinterpret_cast(st_ptr); - uses_allocator_and_not_convertible_arg *uanci_ptr = reinterpret_cast(st_ptr); - uses_allocator_and_convertible_arg *uaci_ptr = reinterpret_cast(st_ptr); - uses_erased_type_allocator *ueta_ptr = reinterpret_cast(st_ptr); - + //////////////////////////////////// //not_uses_allocator + //////////////////////////////////// + not_uses_allocator *nua_ptr = reinterpret_cast(st_ptr); nua_ptr = uninitialized_construct_using_allocator(nua_ptr, alloc_arg_t()); + //////////////////////////////////// //uses_allocator_and_convertible_arg - uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, alloc_arg_t()); - BOOST_TEST(uanci_ptr->allocator_called == false); - BOOST_TEST(uanci_ptr->args_called == 0u); + //////////////////////////////////// + uses_allocator_and_convertible_arg *uaci_ptr = reinterpret_cast(st_ptr); + uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t()); + BOOST_TEST(uaci_ptr->allocator_called == true); + BOOST_TEST(uaci_ptr->args_called == 0u); + uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); + BOOST_TEST(uaci_ptr->allocator_called == true); + BOOST_TEST(uaci_ptr->args_called == 3u); + + uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t(), arg1_t(), arg2_t()); + BOOST_TEST(uaci_ptr->allocator_called == true); + BOOST_TEST(uaci_ptr->args_called == 2u); + + //////////////////////////////////// + //uses_allocator_and_not_convertible_arg + //////////////////////////////////// + uses_allocator_and_not_convertible_arg *uanci_ptr = reinterpret_cast(st_ptr); uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, alloc_arg_t()); BOOST_TEST(uanci_ptr->allocator_called == false); BOOST_TEST(uanci_ptr->args_called == 0u); @@ -164,20 +177,10 @@ int main() BOOST_TEST(uanci_ptr->allocator_called == false); BOOST_TEST(uanci_ptr->args_called == 2u); - //uses_allocator_and_not_convertible_arg - uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t()); - BOOST_TEST(uaci_ptr->allocator_called == true); - BOOST_TEST(uaci_ptr->args_called == 0u); - - uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); - BOOST_TEST(uaci_ptr->allocator_called == true); - BOOST_TEST(uaci_ptr->args_called == 3u); - - uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t(), arg1_t(), arg2_t()); - BOOST_TEST(uaci_ptr->allocator_called == true); - BOOST_TEST(uaci_ptr->args_called == 2u); - + //////////////////////////////////// //uses_erased_type_allocator + //////////////////////////////////// + uses_erased_type_allocator *ueta_ptr = reinterpret_cast(st_ptr); ueta_ptr = uninitialized_construct_using_allocator(ueta_ptr, alloc_arg_t()); BOOST_TEST(ueta_ptr->allocator_called == true); BOOST_TEST(ueta_ptr->args_called == 0u); @@ -197,6 +200,80 @@ int main() ueta_ptr = uninitialized_construct_using_allocator(ueta_ptr, non_alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); BOOST_TEST(ueta_ptr->allocator_called == false); BOOST_TEST(ueta_ptr->args_called == 3u); +} +void test_make_obj() +{ + //////////////////////////////////// + //not_uses_allocator + //////////////////////////////////// + not_uses_allocator d = make_obj_using_allocator(alloc_arg_t()); (void)d; + + //////////////////////////////////// + //uses_allocator_and_convertible_arg + //////////////////////////////////// + uses_allocator_and_convertible_arg uaci = make_obj_using_allocator(alloc_arg_t()); + BOOST_TEST(uaci.allocator_called == true); + BOOST_TEST(uaci.args_called == 0u); + + uaci = make_obj_using_allocator(alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); + BOOST_TEST(uaci.allocator_called == true); + BOOST_TEST(uaci.args_called == 3u); + + uaci = make_obj_using_allocator(alloc_arg_t(), arg1_t(), arg2_t()); + BOOST_TEST(uaci.allocator_called == true); + BOOST_TEST(uaci.args_called == 2u); + + //////////////////////////////////// + //uses_allocator_and_not_convertible_arg + //////////////////////////////////// + uses_allocator_and_not_convertible_arg uanci = make_obj_using_allocator(alloc_arg_t()); + BOOST_TEST(uanci.allocator_called == false); + BOOST_TEST(uanci.args_called == 0u); + + uanci = make_obj_using_allocator(alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); + BOOST_TEST(uanci.allocator_called == false); + BOOST_TEST(uanci.args_called == 3u); + + uanci = make_obj_using_allocator(alloc_arg_t(), arg1_t(), arg2_t()); + BOOST_TEST(uanci.allocator_called == false); + BOOST_TEST(uanci.args_called == 2u); + + uanci = make_obj_using_allocator(non_alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); + BOOST_TEST(uanci.allocator_called == false); + BOOST_TEST(uanci.args_called == 3u); + + uanci = make_obj_using_allocator(non_alloc_arg_t(), arg1_t(), arg2_t()); + BOOST_TEST(uanci.allocator_called == false); + BOOST_TEST(uanci.args_called == 2u); + + //////////////////////////////////// + //uses_erased_type_allocator + //////////////////////////////////// + uses_erased_type_allocator ueta = make_obj_using_allocator(alloc_arg_t()); + BOOST_TEST(ueta.allocator_called == true); + BOOST_TEST(ueta.args_called == 0u); + + ueta = make_obj_using_allocator(alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); + BOOST_TEST(ueta.allocator_called == true); + BOOST_TEST(ueta.args_called == 3u); + + ueta = make_obj_using_allocator(alloc_arg_t(), arg1_t(), arg2_t()); + BOOST_TEST(ueta.allocator_called == true); + BOOST_TEST(ueta.args_called == 2u); + + ueta = make_obj_using_allocator(non_alloc_arg_t(), arg1_t(), arg2_t()); + BOOST_TEST(ueta.allocator_called == false); + BOOST_TEST(ueta.args_called == 2u); + + ueta = make_obj_using_allocator(non_alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); + BOOST_TEST(ueta.allocator_called == false); + BOOST_TEST(ueta.args_called == 3u); +} + +int main() +{ + //test_uninitialized_construct(); + test_make_obj(); return boost::report_errors(); }