-
Notifications
You must be signed in to change notification settings - Fork 107
Closed
Labels
Description
I was implementing stable_insert and my tests kept failing with all kinds of errors. Well, turns out operator[] can invalidate pointers (on clang this happens, not gcc) and probably some other functions also, even if no insertion takes place.
The spec says
If an insertion occurs and results in a rehashing of
the container, all iterators are invalidated. Otherwise
iterators are not affected. References are not
invalidated. Rehashing occurs only if the new
number of elements is greater than @ref capacity().
This happens because emplace/insert reserve memory before checking if the object exists, so reallocation can occur even if it exists.
Version of Boost
1.80 (develop)
Steps necessary to reproduce the problem
object o = {
{ "k1", 1 },
{ "k2", 2 },
{ "k3", 3 } };
fprintf(stderr, "begin %p begin + 1 %p - size %d cap %d\n", o.begin(), o.begin() + 1, (int)o.size(), (int)o.capacity());
auto& v = o["k2"];
fprintf(stderr, "begin %p begin + 1 %p - size %d cap %d\n", o.begin(), o.begin() + 1, (int)o.size(), (int)o.capacity());example output
begin 0x60d000024af0 begin + 1 0x60d000024b18 - size 3 cap 3
begin 0x60f0000041f0 begin + 1 0x60f000004218 - size 3 cap 4
All relevant compiler information
$ clang --version
clang version 11.0.0
Target: x86_64-pc-linux-gnu