From 7635eba375c2eda18307db78515cce0adc76c9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sat, 17 Jan 2015 19:11:52 +0100 Subject: [PATCH] Typedef'ed allocator_arg_t as "const std::allocator_arg_t &" to improve interoperability between C++11 allocator-aware types. --- .../container/detail/std_allocator_arg.hpp | 33 --- include/boost/container/scoped_allocator.hpp | 60 +---- .../boost/container/scoped_allocator_fwd.hpp | 35 ++- test/scoped_allocator_adaptor_test.cpp | 246 ------------------ 4 files changed, 28 insertions(+), 346 deletions(-) delete mode 100644 include/boost/container/detail/std_allocator_arg.hpp diff --git a/include/boost/container/detail/std_allocator_arg.hpp b/include/boost/container/detail/std_allocator_arg.hpp deleted file mode 100644 index 9b79c50..0000000 --- a/include/boost/container/detail/std_allocator_arg.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef BOOST_CONTAINER_DETAIL_ALLOCATOR_ARG_HPP -#define BOOST_CONTAINER_DETAIL_ALLOCATOR_ARG_HPP -/////////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2014-2015. Distributed under the Boost -// Software License, Version 1.0. (See accompanying file -// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/container for documentation. -// -/////////////////////////////////////////////////////////////////////////////// - -#if defined(BOOST_HAS_PRAGMA_ONCE) -# pragma once -#endif - -#include - -namespace boost { namespace container { - -template -struct alloc_arg -{ - static const std::allocator_arg_t &get() { return *palloc_arg; } - static std::allocator_arg_t *palloc_arg; -}; - -template -std::allocator_arg_t *alloc_arg::palloc_arg; - -}} //namespace boost { namespace container { - -#endif //BOOST_CONTAINER_DETAIL_ALLOCATOR_ARG_HPP diff --git a/include/boost/container/scoped_allocator.hpp b/include/boost/container/scoped_allocator.hpp index a69029f..dee2b32 100644 --- a/include/boost/container/scoped_allocator.hpp +++ b/include/boost/container/scoped_allocator.hpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) @@ -136,10 +135,6 @@ template struct constructible_with_allocator_prefix { static const bool value = false; }; -template -struct constructible_with_std_allocator_prefix -{ static const bool value = false; }; - #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED namespace container_detail { @@ -297,11 +292,6 @@ namespace container_detail { : is_constructible {}; - template - struct is_constructible_with_std_allocator_prefix - : is_constructible - {}; - #else // #if !defined(BOOST_NO_SFINAE_EXPR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) //Without advanced SFINAE expressions, we can't use is_constructible @@ -314,11 +304,6 @@ namespace container_detail { : constructible_with_allocator_prefix {}; - template - struct is_constructible_with_std_allocator_prefix - : constructible_with_std_allocator_prefix - {}; - template struct is_constructible_with_allocator_suffix : constructible_with_allocator_suffix @@ -331,11 +316,6 @@ namespace container_detail { : constructible_with_allocator_prefix {}; - template - struct is_constructible_with_std_allocator_prefix - : constructible_with_std_allocator_prefix - {}; - template struct is_constructible_with_allocator_suffix : constructible_with_allocator_suffix @@ -354,27 +334,14 @@ template < typename OutermostAlloc , class ...Args > inline void dispatch_allocator_prefix_suffix - ( unsigned_<1> use_alloc_prefix, OutermostAlloc& outermost_alloc + ( true_type use_alloc_prefix, OutermostAlloc& outermost_alloc , InnerAlloc& inner_alloc, T* p, BOOST_FWD_REF(Args) ...args) { (void)use_alloc_prefix; allocator_traits::construct ( outermost_alloc, p, allocator_arg, inner_alloc, ::boost::forward(args)...); } -// std::allocator_arg_t -template < typename OutermostAlloc - , typename InnerAlloc - , typename T - , class ...Args - > -inline void dispatch_allocator_prefix_suffix - ( unsigned_<2> use_alloc_prefix, OutermostAlloc& outermost_alloc - , InnerAlloc &inner_alloc, T* p, BOOST_FWD_REF(Args)...args) -{ - (void)use_alloc_prefix; - allocator_traits::construct - ( outermost_alloc, p, alloc_arg<>::get(), inner_alloc, ::boost::forward(args)...); -} + // allocator suffix template < typename OutermostAlloc , typename InnerAlloc @@ -382,7 +349,7 @@ template < typename OutermostAlloc , class ...Args > inline void dispatch_allocator_prefix_suffix - ( unsigned_<0> use_alloc_prefix, OutermostAlloc& outermost_alloc + ( false_type use_alloc_prefix, OutermostAlloc& outermost_alloc , InnerAlloc &inner_alloc, T* p, BOOST_FWD_REF(Args)...args) { (void)use_alloc_prefix; @@ -403,8 +370,7 @@ inline void dispatch_uses_allocator //BOOST_STATIC_ASSERT((is_constructible_with_allocator_prefix::value || // is_constructible_with_allocator_suffix::value )); dispatch_allocator_prefix_suffix - ( unsigned_< is_constructible_with_allocator_prefix::value ? 1u : - (is_constructible_with_std_allocator_prefix::value ? 2u : 0u) >() + ( bool_< is_constructible_with_allocator_prefix::value>() , outermost_alloc, inner_alloc, p, ::boost::forward(args)...); } @@ -429,7 +395,7 @@ inline void dispatch_uses_allocator template < typename OutermostAlloc, typename InnerAlloc, typename T\ BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \ inline void dispatch_allocator_prefix_suffix\ - (unsigned_<1u> use_alloc_prefix, OutermostAlloc& outermost_alloc,\ + (true_type use_alloc_prefix, OutermostAlloc& outermost_alloc,\ InnerAlloc& inner_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ {\ (void)use_alloc_prefix,\ @@ -437,21 +403,10 @@ inline void dispatch_allocator_prefix_suffix\ (outermost_alloc, p, allocator_arg, inner_alloc BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ }\ \ -template < typename OutermostAlloc, typename InnerAlloc, typename T\ - BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \ -inline void dispatch_allocator_prefix_suffix\ - (unsigned_<2u> use_alloc_prefix, OutermostAlloc& outermost_alloc,\ - InnerAlloc& inner_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ -{\ - (void)use_alloc_prefix,\ - allocator_traits::construct\ - (outermost_alloc, p, alloc_arg<>::get(), inner_alloc BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ -}\ -\ template < typename OutermostAlloc, typename InnerAlloc, typename T\ BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\ inline void dispatch_allocator_prefix_suffix\ - (unsigned_<0u> use_alloc_prefix, OutermostAlloc& outermost_alloc,\ + (false_type use_alloc_prefix, OutermostAlloc& outermost_alloc,\ InnerAlloc& inner_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ {\ (void)use_alloc_prefix;\ @@ -467,8 +422,7 @@ inline void dispatch_uses_allocator\ {\ (void)uses_allocator;\ dispatch_allocator_prefix_suffix\ - ( unsigned_< is_constructible_with_allocator_prefix::value ? 1u :\ - (is_constructible_with_std_allocator_prefix::value ? 2u : 0u) >()\ + ( bool_< is_constructible_with_allocator_prefix::value >()\ , outermost_alloc, inner_alloc, p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\ }\ \ diff --git a/include/boost/container/scoped_allocator_fwd.hpp b/include/boost/container/scoped_allocator_fwd.hpp index f33f800..f55f89f 100644 --- a/include/boost/container/scoped_allocator_fwd.hpp +++ b/include/boost/container/scoped_allocator_fwd.hpp @@ -35,38 +35,48 @@ namespace boost { namespace container { #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) - template - class scoped_allocator_adaptor; + template + class scoped_allocator_adaptor; #else // #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) - template - class scoped_allocator_adaptor; + template + class scoped_allocator_adaptor; - template - class scoped_allocator_adaptor; + template + class scoped_allocator_adaptor; #endif // #if !defined(BOOST_CONTAINER_UNIMPLEMENTED_PACK_EXPANSION_TO_FIXED_LIST) - #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) -template -class scoped_allocator_adaptor; + template + class scoped_allocator_adaptor; #endif + template + struct std_allocator_arg_holder + { + static ::std::allocator_arg_t *dummy; + }; + + template + ::std::allocator_arg_t *std_allocator_arg_holder::dummy; + +#else //BOOST_CONTAINER_DOXYGEN_INVOKED + #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED //! The allocator_arg_t struct is an empty structure type used as a unique type to //! disambiguate constructor and function overloading. Specifically, several types //! have constructors with allocator_arg_t as the first argument, immediately followed //! by an argument of a type that satisfies Allocator requirements -struct allocator_arg_t{}; +typedef const std::allocator_arg_t & allocator_arg_t; //! A instance of type allocator_arg_t //! -static const allocator_arg_t allocator_arg = allocator_arg_t(); +static allocator_arg_t allocator_arg = BOOST_CONTAINER_DOC1ST(unspecified, *std_allocator_arg_holder<>::dummy); template struct constructible_with_allocator_suffix; @@ -74,9 +84,6 @@ struct constructible_with_allocator_suffix; template struct constructible_with_allocator_prefix; -template -struct constructible_with_std_allocator_prefix; - template struct uses_allocator; diff --git a/test/scoped_allocator_adaptor_test.cpp b/test/scoped_allocator_adaptor_test.cpp index fb55d22..3d63ed5 100644 --- a/test/scoped_allocator_adaptor_test.cpp +++ b/test/scoped_allocator_adaptor_test.cpp @@ -117,7 +117,6 @@ struct mark_on_destructor enum ConstructionTypeEnum { ConstructiblePrefix, - ConstructibleStdPrefix, ConstructibleSuffix, NotUsesAllocator }; @@ -138,8 +137,6 @@ struct uses_allocator_base typedef allocator_type allocator_constructor_type; struct nat{}; typedef nat allocator_arg_type; - struct nat2{}; - typedef nat2 std_allocator_arg_type; }; template @@ -148,18 +145,6 @@ struct uses_allocator_base typedef test_allocator allocator_type; typedef allocator_type allocator_constructor_type; typedef allocator_arg_t allocator_arg_type; - struct nat{}; - typedef nat std_allocator_arg_type; -}; - -template -struct uses_allocator_base -{ - typedef test_allocator allocator_type; - typedef allocator_type allocator_constructor_type; - struct nat{}; - typedef nat allocator_arg_type; - typedef const std::allocator_arg_t& std_allocator_arg_type; }; template @@ -168,8 +153,6 @@ struct uses_allocator_base struct nat{}; typedef nat allocator_constructor_type; typedef nat allocator_arg_type; - struct nat2{}; - typedef nat2 std_allocator_arg_type; }; template @@ -198,11 +181,6 @@ struct mark_on_scoped_allocation : construction_type(ConstructiblePrefix), value(0) {} - explicit mark_on_scoped_allocation - (typename base_type::std_allocator_arg_type, typename base_type::allocator_constructor_type) - : construction_type(ConstructibleStdPrefix), value(0) - {} - //1 user argument constructors explicit mark_on_scoped_allocation(int i) : construction_type(NotUsesAllocator), value(i) @@ -220,13 +198,6 @@ struct mark_on_scoped_allocation : construction_type(ConstructiblePrefix), value(i) {} - mark_on_scoped_allocation - ( typename base_type::std_allocator_arg_type - , typename base_type::allocator_constructor_type - , int i) - : construction_type(ConstructibleStdPrefix), value(i) - {} - //Copy constructors mark_on_scoped_allocation(const mark_on_scoped_allocation &other) : construction_type(NotUsesAllocator), value(other.value) @@ -243,12 +214,6 @@ struct mark_on_scoped_allocation : construction_type(ConstructiblePrefix), value(other.value) {} - mark_on_scoped_allocation( typename base_type::std_allocator_arg_type - , typename base_type::allocator_constructor_type - , const mark_on_scoped_allocation &other) - : construction_type(ConstructibleStdPrefix), value(other.value) - {} - //Move constructors mark_on_scoped_allocation(BOOST_RV_REF(mark_on_scoped_allocation) other) : construction_type(NotUsesAllocator), value(other.value) @@ -265,12 +230,6 @@ struct mark_on_scoped_allocation : construction_type(ConstructiblePrefix), value(other.value) { other.value = 0; other.construction_type = ConstructiblePrefix; } - mark_on_scoped_allocation( typename base_type::std_allocator_arg_type - , typename base_type::allocator_constructor_type - , BOOST_RV_REF(mark_on_scoped_allocation) other) - : construction_type(ConstructibleStdPrefix), value(other.value) - { other.value = 0; other.construction_type = ConstructibleStdPrefix; } - ConstructionTypeEnum construction_type; int value; }; @@ -285,13 +244,6 @@ struct constructible_with_allocator_prefix static const bool value = true; }; -template -struct constructible_with_std_allocator_prefix - < ::mark_on_scoped_allocation > -{ - static const bool value = true; -}; - template struct constructible_with_allocator_suffix < ::mark_on_scoped_allocation > @@ -819,18 +771,12 @@ int main() < ::mark_on_scoped_allocation , test_allocator >::value )); - BOOST_STATIC_ASSERT(( boost::container::uses_allocator - < ::mark_on_scoped_allocation - , test_allocator - >::value )); BOOST_STATIC_ASSERT(( boost::container::uses_allocator < ::mark_on_scoped_allocation , test_allocator >::value )); BOOST_STATIC_ASSERT(( boost::container::constructible_with_allocator_prefix < ::mark_on_scoped_allocation >::value )); - BOOST_STATIC_ASSERT(( boost::container::constructible_with_std_allocator_prefix - < ::mark_on_scoped_allocation >::value )); BOOST_STATIC_ASSERT(( boost::container::constructible_with_allocator_suffix < ::mark_on_scoped_allocation >::value )); @@ -878,18 +824,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - s0i.construct(&dummy); - if(dummy.construction_type != ConstructibleStdPrefix || - dummy.value != 0){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } //Check construction with 1 user arguments { @@ -928,18 +862,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - s0i.construct(&dummy, 3); - if(dummy.construction_type != ConstructibleStdPrefix || - dummy.value != 3){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } } //////////////////////////////////////////////////////////// //Then check scoped allocator with OuterAlloc and InnerAlloc. @@ -985,18 +907,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - s1i.construct(&dummy); - if(dummy.construction_type != ConstructibleStdPrefix || - dummy.value != 0){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } //Check construction with 1 user arguments { @@ -1035,18 +945,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - s1i.construct(&dummy, 3); - if(dummy.construction_type != ConstructibleStdPrefix || - dummy.value != 3){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } } ////////////////////////////////////////////////////////////////////////////////// @@ -1114,18 +1012,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - ssro0i.construct(&dummy); - if(dummy.construction_type != ConstructibleStdPrefix || - dummy.value != 0){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } //Check construction with 1 user arguments { @@ -1164,18 +1050,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - ssro0i.construct(&dummy, 3); - if(dummy.construction_type != ConstructibleStdPrefix || - dummy.value != 3){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } } //////////////////////////////////////////////////////////// //Then check scoped allocator with OuterAlloc and InnerAlloc. @@ -1245,18 +1119,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - ssro1i.construct(&dummy); - if(dummy.construction_type != NotUsesAllocator || - dummy.value != 0){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } //Check construction with 1 user arguments { @@ -1295,18 +1157,6 @@ int main() } dummy.~MarkType(); } - { - typedef ::mark_on_scoped_allocation MarkType; - MarkType dummy; - dummy.~MarkType(); - ssro1i.construct(&dummy, 3); - if(dummy.construction_type != NotUsesAllocator || - dummy.value != 3){ - dummy.~MarkType(); - return 1; - } - dummy.~MarkType(); - } } //////////////////////////////////////////////////////////// @@ -1370,21 +1220,6 @@ int main() } dummy.~MarkTypePair(); } - { - typedef ::mark_on_scoped_allocation MarkType; - typedef pair MarkTypePair; - MarkTypePair dummy; - dummy.~MarkTypePair(); - s0i.construct(&dummy); - if(dummy.first.construction_type != ConstructibleStdPrefix || - dummy.second.construction_type != ConstructibleStdPrefix || - dummy.first.value != 0 || - dummy.second.value != 0 ){ - dummy.~MarkTypePair(); - return 1; - } - dummy.~MarkTypePair(); - } //Check construction with 1 user arguments for each pair { @@ -1432,21 +1267,6 @@ int main() } dummy.~MarkTypePair(); } - { - typedef ::mark_on_scoped_allocation MarkType; - typedef pair MarkTypePair; - MarkTypePair dummy; - dummy.~MarkTypePair(); - s0i.construct(&dummy, 2, 2); - if(dummy.first.construction_type != ConstructibleStdPrefix || - dummy.second.construction_type != ConstructibleStdPrefix || - dummy.first.value != 2 || - dummy.second.value != 2 ){ - dummy.~MarkTypePair(); - return 1; - } - dummy.~MarkTypePair(); - } //Check construction with pair copy construction { typedef ::mark_on_scoped_allocation MarkType; @@ -1493,21 +1313,6 @@ int main() } dummy.~MarkTypePair(); } - { - typedef ::mark_on_scoped_allocation MarkType; - typedef pair MarkTypePair; - MarkTypePair dummy, dummy2(2, 2); - dummy.~MarkTypePair(); - s0i.construct(&dummy, dummy2); - if(dummy.first.construction_type != ConstructibleStdPrefix || - dummy.second.construction_type != ConstructibleStdPrefix || - dummy.first.value != 2 || - dummy.second.value != 2 ){ - dummy.~MarkTypePair(); - return 1; - } - dummy.~MarkTypePair(); - } //Check construction with pair move construction { typedef ::mark_on_scoped_allocation MarkType; @@ -1567,25 +1372,6 @@ int main() } dummy.~MarkTypePair(); } - { - typedef ::mark_on_scoped_allocation MarkType; - typedef pair MarkTypePair; - MarkTypePair dummy, dummy2(2, 2); - dummy.~MarkTypePair(); - s0i.construct(&dummy, ::boost::move(dummy2)); - if(dummy.first.construction_type != ConstructibleStdPrefix || - dummy.second.construction_type != ConstructibleStdPrefix || - dummy.first.value != 2 || - dummy.second.value != 2 || - dummy2.first.construction_type != ConstructibleStdPrefix || - dummy2.second.construction_type != ConstructibleStdPrefix || - dummy2.first.value != 0 || - dummy2.second.value != 0 ){ - dummy2.~MarkTypePair(); - return 1; - } - dummy.~MarkTypePair(); - } //Check construction with related pair copy construction { typedef ::mark_on_scoped_allocation MarkType; @@ -1635,22 +1421,6 @@ int main() } dummy.~MarkTypePair(); } - { - typedef ::mark_on_scoped_allocation MarkType; - typedef pair MarkTypePair; - MarkTypePair dummy; - pair dummy2(2, 2); - dummy.~MarkTypePair(); - s0i.construct(&dummy, dummy2); - if(dummy.first.construction_type != ConstructibleStdPrefix || - dummy.second.construction_type != ConstructibleStdPrefix || - dummy.first.value != 2 || - dummy.second.value != 2 ){ - dummy.~MarkTypePair(); - return 1; - } - dummy.~MarkTypePair(); - } //Check construction with related pair move construction { typedef ::mark_on_scoped_allocation MarkType; @@ -1700,22 +1470,6 @@ int main() } dummy.~MarkTypePair(); } - { - typedef ::mark_on_scoped_allocation MarkType; - typedef pair MarkTypePair; - MarkTypePair dummy; - pair dummy2(2, 2); - dummy.~MarkTypePair(); - s0i.construct(&dummy, ::boost::move(dummy2)); - if(dummy.first.construction_type != ConstructibleStdPrefix || - dummy.second.construction_type != ConstructibleStdPrefix || - dummy.first.value != 2 || - dummy.second.value != 2 ){ - dummy.~MarkTypePair(); - return 1; - } - dummy.~MarkTypePair(); - } } }