Skip to main content
Add missing word
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

I'm very confused by the fact that your class allows you to modify the keys stored inside an std::set by const_casting the pointer into said std::set.

I don't use STL so my knowledge is shallow at best, but this SO thread seems to agree that you must always erase and re-insert the key if you want to modify it.

My guess is that this code

sb["banana"] = 25;

// ...

sb["banana"] = 15;
sb[15] = "melon";

// ...

SSVUT_EXPECT(!sb.has("banana"));

only works because at this point of your test the sets contain just one item, so the red/black trees inside of them have no opportunity to get corrupted.

I'm very confused by the fact that your class allows to modify the keys stored inside an std::set by const_casting the pointer into said std::set.

I don't use STL so my knowledge is shallow at best, but this SO thread seems to agree that you must always erase and re-insert the key if you want to modify it.

My guess is that this code

sb["banana"] = 25;

// ...

sb["banana"] = 15;
sb[15] = "melon";

// ...

SSVUT_EXPECT(!sb.has("banana"));

only works because at this point of your test the sets contain just one item, so the red/black trees inside of them have no opportunity to get corrupted.

I'm very confused by the fact that your class allows you to modify the keys stored inside an std::set by const_casting the pointer into said std::set.

I don't use STL so my knowledge is shallow at best, but this SO thread seems to agree that you must always erase and re-insert the key if you want to modify it.

My guess is that this code

sb["banana"] = 25;

// ...

sb["banana"] = 15;
sb[15] = "melon";

// ...

SSVUT_EXPECT(!sb.has("banana"));

only works because at this point of your test the sets contain just one item, so the red/black trees inside of them have no opportunity to get corrupted.

Source Link

I'm very confused by the fact that your class allows to modify the keys stored inside an std::set by const_casting the pointer into said std::set.

I don't use STL so my knowledge is shallow at best, but this SO thread seems to agree that you must always erase and re-insert the key if you want to modify it.

My guess is that this code

sb["banana"] = 25;

// ...

sb["banana"] = 15;
sb[15] = "melon";

// ...

SSVUT_EXPECT(!sb.has("banana"));

only works because at this point of your test the sets contain just one item, so the red/black trees inside of them have no opportunity to get corrupted.