From 49440c3525a2f1136fd9cc0236fc85b2fa411168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Tue, 10 Sep 2024 23:50:24 +0200 Subject: [PATCH] Add compile-time check to make sure allocator_type::value_type and container's value_type are the same type. --- include/boost/container/deque.hpp | 5 +++++ include/boost/container/detail/flat_tree.hpp | 7 ++++++- include/boost/container/detail/tree.hpp | 4 ++++ include/boost/container/devector.hpp | 5 +++++ include/boost/container/list.hpp | 4 ++++ include/boost/container/slist.hpp | 5 +++++ include/boost/container/stable_vector.hpp | 5 +++++ include/boost/container/string.hpp | 5 +++++ include/boost/container/vector.hpp | 5 ++++- 9 files changed, 43 insertions(+), 2 deletions(-) diff --git a/include/boost/container/deque.hpp b/include/boost/container/deque.hpp index da24ad0d..c1cb4c42 100644 --- a/include/boost/container/deque.hpp +++ b/include/boost/container/deque.hpp @@ -1026,6 +1026,11 @@ class deque : protected deque_base::type, #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED private: // Internal typedefs + + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value_type>::value)); + BOOST_COPYABLE_AND_MOVABLE(deque) typedef typename Base::ptr_alloc_ptr index_pointer; typedef allocator_traits allocator_traits_type; diff --git a/include/boost/container/detail/flat_tree.hpp b/include/boost/container/detail/flat_tree.hpp index a1de8ab8..64ab444c 100644 --- a/include/boost/container/detail/flat_tree.hpp +++ b/include/boost/container/detail/flat_tree.hpp @@ -592,6 +592,10 @@ class flat_tree typedef typename container_type::reverse_iterator reverse_iterator; typedef typename container_type::const_reverse_iterator const_reverse_iterator; + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((is_same::value)); + //!Standard extension typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT (boost::container::dtl::, container_type @@ -733,7 +737,8 @@ class flat_tree } inline ~flat_tree() - {} + { + } inline flat_tree& operator=(BOOST_COPY_ASSIGN_REF(flat_tree) x) { m_data = x.m_data; return *this; } diff --git a/include/boost/container/detail/tree.hpp b/include/boost/container/detail/tree.hpp index 1a3aa421..43379247 100644 --- a/include/boost/container/detail/tree.hpp +++ b/include/boost/container/detail/tree.hpp @@ -456,6 +456,10 @@ class tree private: + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value_type>::value)); + typedef key_node_pred KeyNodeCompare; public: diff --git a/include/boost/container/devector.hpp b/include/boost/container/devector.hpp index e7fe1df5..5d9fb7be 100644 --- a/include/boost/container/devector.hpp +++ b/include/boost/container/devector.hpp @@ -173,6 +173,11 @@ class devector #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED private: + + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value_type>::value)); + BOOST_COPYABLE_AND_MOVABLE(devector) // Guard to deallocate buffer on exception diff --git a/include/boost/container/list.hpp b/include/boost/container/list.hpp index e597c487..1061446f 100644 --- a/include/boost/container/list.hpp +++ b/include/boost/container/list.hpp @@ -1378,6 +1378,10 @@ class list #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED private: + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value)); + void priv_move_assign(BOOST_RV_REF(list) x, dtl::bool_ /*steal_resources*/) { //Destroy objects but retain memory in case x reuses it in the future diff --git a/include/boost/container/slist.hpp b/include/boost/container/slist.hpp index b8853f56..54ef5bde 100644 --- a/include/boost/container/slist.hpp +++ b/include/boost/container/slist.hpp @@ -1565,6 +1565,11 @@ class slist #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED private: + + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value)); + void priv_move_assign(BOOST_RV_REF(slist) x, dtl::bool_ /*steal_resources*/) { //Destroy objects but retain memory in case x reuses it in the future diff --git a/include/boost/container/stable_vector.hpp b/include/boost/container/stable_vector.hpp index e0799129..aa315b01 100644 --- a/include/boost/container/stable_vector.hpp +++ b/include/boost/container/stable_vector.hpp @@ -597,6 +597,11 @@ class stable_vector #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED private: + + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value_type>::value)); + BOOST_COPYABLE_AND_MOVABLE(stable_vector) BOOST_STATIC_CONSTEXPR size_type ExtraPointers = index_traits_type::ExtraPointers; diff --git a/include/boost/container/string.hpp b/include/boost/container/string.hpp index 25723a1a..4a69e192 100644 --- a/include/boost/container/string.hpp +++ b/include/boost/container/string.hpp @@ -628,6 +628,11 @@ class basic_string #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED private: + + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value_type>::value)); + typedef constant_iterator cvalue_iterator; typedef typename base_t::alloc_version alloc_version; typedef ::boost::intrusive::pointer_traits pointer_traits; diff --git a/include/boost/container/vector.hpp b/include/boost/container/vector.hpp index 0f683c2e..b8542e5c 100644 --- a/include/boost/container/vector.hpp +++ b/include/boost/container/vector.hpp @@ -813,8 +813,11 @@ class vector typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator) const_reverse_iterator; private: - #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + //`allocator_type::value_type` must match container's `value type`. If this + //assertion fails, please review your allocator definition. + BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same::value)); + typedef typename boost::container:: allocator_traits::size_type alloc_size_type; typedef typename get_vector_opt::type options_type;