Просмотр исходного кода

memory: review propagate_on_container_swap

Michele Caini 3 лет назад
Родитель
Сommit
7c58c83ee0
3 измененных файлов с 14 добавлено и 4 удалено
  1. 2 2
      src/entt/core/memory.hpp
  2. 6 2
      test/entt/common/basic_test_allocator.hpp
  3. 6 0
      test/entt/core/memory.cpp

+ 2 - 2
src/entt/core/memory.hpp

@@ -60,11 +60,11 @@ constexpr void propagate_on_container_move_assignment([[maybe_unused]] Allocator
  */
 template<typename Allocator>
 constexpr void propagate_on_container_swap([[maybe_unused]] Allocator &lhs, [[maybe_unused]] Allocator &rhs) noexcept {
-    ENTT_ASSERT(std::allocator_traits<Allocator>::propagate_on_container_swap::value || lhs == rhs, "Cannot swap the containers");
-
     if constexpr(std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
         using std::swap;
         swap(lhs, rhs);
+    } else {
+        ENTT_ASSERT(lhs == rhs, "Cannot swap the containers");
     }
 }
 

+ 6 - 2
test/entt/common/basic_test_allocator.hpp

@@ -6,13 +6,13 @@
 
 namespace test {
 
-template<typename Type>
+template<typename Type, typename Pocs = std::true_type>
 struct basic_test_allocator: std::allocator<Type> {
     // basic pocca/pocma/pocs allocator
 
     using base = std::allocator<Type>;
     using propagate_on_container_copy_assignment = std::true_type;
-    using propagate_on_container_swap = std::true_type;
+    using propagate_on_container_swap = Pocs;
 
     using std::allocator<Type>::allocator;
 
@@ -21,6 +21,10 @@ struct basic_test_allocator: std::allocator<Type> {
         base::operator=(other);
         return *this;
     }
+
+    bool operator==(const basic_test_allocator &other) {
+        return (this == &other);
+    }
 };
 
 } // namespace test

+ 6 - 0
test/entt/core/memory.cpp

@@ -30,6 +30,12 @@ TEST(PoccaPocmaAndPocs, Functionalities) {
     entt::propagate_on_container_swap(lhs, rhs);
 }
 
+ENTT_DEBUG_TEST(PoccaPocmaAndPocsDeathTest, Functionalities) {
+    using pocs = std::false_type;
+    test::basic_test_allocator<int, pocs> lhs, rhs;
+    ASSERT_DEATH(entt::propagate_on_container_swap(lhs, rhs), "");
+}
+
 TEST(IsPowerOfTwo, Functionalities) {
     // constexpr-ness guaranteed
     constexpr auto zero_is_power_of_two = entt::is_power_of_two(0u);