dense_set: strong exception guarantee (emplace/insert)

This commit is contained in:
Michele Caini
2022-03-30 15:34:45 +02:00
parent 335f876a46
commit b63aebfdaf
2 changed files with 17 additions and 2 deletions

View File

@@ -275,8 +275,8 @@ class dense_set {
return std::make_pair(it, false);
}
const auto next = std::exchange(sparse.first()[index], packed.first().size());
packed.first().emplace_back(next, std::forward<Other>(value));
packed.first().emplace_back(sparse.first()[index], std::forward<Other>(value));
sparse.first()[index] = packed.first().size() - 1u;
rehash_if_required();
return std::make_pair(--end(), true);

View File

@@ -832,6 +832,21 @@ TEST(DenseSet, ThrowingAllocator) {
ASSERT_EQ(set.bucket_count(), minimum_bucket_count);
ASSERT_THROW(set.reserve(2u * set.bucket_count()), packed_exception);
ASSERT_EQ(set.bucket_count(), minimum_bucket_count);
packed_allocator::trigger_on_allocate = true;
ASSERT_THROW(set.emplace(), packed_exception);
ASSERT_FALSE(set.contains(0u));
packed_allocator::trigger_on_allocate = true;
ASSERT_THROW(set.emplace(std::size_t{}), packed_exception);
ASSERT_FALSE(set.contains(0u));
packed_allocator::trigger_on_allocate = true;
ASSERT_THROW(set.insert(0u), packed_exception);
ASSERT_FALSE(set.contains(0u));
}
#if defined(ENTT_HAS_TRACKED_MEMORY_RESOURCE)