Assert failure when implementing `erase_if()` for `small::map`
Closed this issue · 1 comments
tearfur commented
The problem
Steps to Reproduce
CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(small_test)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(small)
add_executable(small_test main.cpp)
target_link_libraries(small_test PUBLIC small::small)
#include <small/map.hpp>
int main() {
small::map<int, int> map{{1, 1}, {2, 2}, {3, 3}};
for (auto iter = std::begin(map), end = std::end(map); iter != end;)
{
auto const& [key, val] = *iter;
if (val == 3)
iter = map.erase(iter);
else
++iter;
}
return 0;
}
Output
small_test: /path/to/small-test/small/include/small/vector.hpp:1205: void small::vector<T, N, Allocator, AllowHeap, SizeType, GrowthFactor>::set_internal_size(std::size_t) [with T = std::pair<int, int>; long unsigned int N = 5; Allocator = std::allocator<std::pair<int, int> >; AllowHeap = std::integral_constant<bool, true>; SizeType = long unsigned int; GrowthFactor = std::ratio<3, 2>; std::size_t = long unsigned int]: Assertion `sz <= clear_size_mask' failed.
Platform
- cross-platform issue - linux
- cross-platform issue - windows
- cross-platform issue - macos
- other: ___________
Environment Details
- OS: Ubuntu
- OS Version: 22.04
- Compiler: g++
- Compiler version: Ubuntu 11.4.0-1ubuntu1~22.04
Proposed solution
Alternatives I've considered
Additional context
tearfur commented
This works fine on the other hand. Is it by design that erasing elements from small::map
invalidates iterators other than the one being erased?
#include <small/map.hpp>
int main() {
small::map<int, int> map{{1, 1}, {2, 2}, {3, 3}};
for (auto iter = std::begin(map); iter != std::end(map);)
{
auto const& [key, val] = *iter;
if (val == 3)
iter = map.erase(iter);
else
++iter;
}
return 0;
}