Browse Source

dense_map: constrain the allocator type

Michele Caini 4 years ago
parent
commit
dcb7c0c27e
2 changed files with 5 additions and 4 deletions
  1. 1 0
      src/entt/container/dense_map.hpp
  2. 4 4
      test/entt/container/dense_map.cpp

+ 1 - 0
src/entt/container/dense_map.hpp

@@ -263,6 +263,7 @@ class dense_map {
 
     using node_type = internal::dense_map_node<Key, Type>;
     using alloc_traits = typename std::allocator_traits<Allocator>;
+    static_assert(std::is_same_v<typename alloc_traits::value_type, std::pair<const Key, Type>>);
     using sparse_container_type = std::vector<std::size_t, typename alloc_traits::template rebind_alloc<std::size_t>>;
     using packed_container_type = std::vector<node_type, typename alloc_traits::template rebind_alloc<node_type>>;
 

+ 4 - 4
test/entt/container/dense_map.cpp

@@ -1080,7 +1080,7 @@ TEST(DenseMap, Reserve) {
 }
 
 TEST(DenseMap, ThrowingAllocator) {
-    using allocator = test::throwing_allocator<std::pair<std::size_t, std::size_t>>;
+    using allocator = test::throwing_allocator<std::pair<const std::size_t, std::size_t>>;
     using packed_allocator = test::throwing_allocator<entt::internal::dense_map_node<std::size_t, std::size_t>>;
     using packed_exception = typename packed_allocator::exception_type;
 
@@ -1097,7 +1097,7 @@ TEST(DenseMap, ThrowingAllocator) {
 #if defined(ENTT_HAS_TRACKED_MEMORY_RESOURCE)
 
 TEST(DenseMap, NoUsesAllocatorConstruction) {
-    using allocator = std::pmr::polymorphic_allocator<std::pair<int, int>>;
+    using allocator = std::pmr::polymorphic_allocator<std::pair<const int, int>>;
 
     test::tracked_memory_resource memory_resource{};
     entt::dense_map<int, int, std::hash<int>, std::equal_to<int>, allocator> map{&memory_resource};
@@ -1113,7 +1113,7 @@ TEST(DenseMap, NoUsesAllocatorConstruction) {
 
 TEST(DenseMap, KeyUsesAllocatorConstruction) {
     using string_type = typename test::tracked_memory_resource::string_type;
-    using allocator = std::pmr::polymorphic_allocator<std::pair<string_type, int>>;
+    using allocator = std::pmr::polymorphic_allocator<std::pair<const string_type, int>>;
 
     test::tracked_memory_resource memory_resource{};
     entt::dense_map<string_type, int, std::hash<string_type>, std::equal_to<string_type>, allocator> map{&memory_resource};
@@ -1136,7 +1136,7 @@ TEST(DenseMap, KeyUsesAllocatorConstruction) {
 
 TEST(DenseMap, ValueUsesAllocatorConstruction) {
     using string_type = typename test::tracked_memory_resource::string_type;
-    using allocator = std::pmr::polymorphic_allocator<std::pair<int, string_type>>;
+    using allocator = std::pmr::polymorphic_allocator<std::pair<const int, string_type>>;
 
     test::tracked_memory_resource memory_resource{};
     entt::dense_map<int, string_type, std::hash<int>, std::equal_to<int>, allocator> map{std::pmr::get_default_resource()};