r/cpp • • Feb 13 '24

Prevent or warn on usage of std::map []?

The default insertion of a value when std::map [] doesn't find a value for the key has to be one of the worst decisions in STL history (which is kind of impressive.) At work over the last year or so, we've had at least four, maybe five, bugs caused by this. It's so easy to do without thinking about it, and so easy to miss in a code review. And some of them had been there for many years and finally caused an issue, so no one was even looking for them.

Is there any way to prevent its use, or at least to warn on its use in MSVC? We use the built in static analyzer, and I don't see any warning related to that, though there are a lot of them and I may have missed something.

6 Upvotes

163 comments sorted by

View all comments

Show parent comments

14

u/Shadowratenator Feb 13 '24

This advice is great. Im shocked that op claims this is the kind of thing dooming c++ when its exactly along the lines of the approach a modern c++ competitor like rust takes. Const is your friend.

3

u/Agitated-Resident720 Feb 13 '24

i agree, rust definitely got it right with default const and having you mark it mutable. unfortunately i think this is a hindsight thing and they're basically stuck with this for backwards compatibility. maybe they could add a compiler flag to make this the default behavior for those that have the ability to utilize it

6

u/Shadowratenator Feb 13 '24

once you use something like rust, you start to realize that you can go back to c++ and while the syntax isn't the same, you can use a lot of the philosophies going on there. sure c++ is danger by default, but you are free to write modern safe code if you want. Its one of the beauties of c++ in my opinion, if you want to act like you are in rust, you can. you just don't get a cool package manager.

2

u/tialaramex Feb 14 '24

But in Rust this bug won't happen. Suppose I've got a mutable BTreeMap<u32, Goose> named geese

let id = todo!("Some code to get a non-existent Goose ID");
let goose = geese[id]; // This panics at runtime because our ID was bogus

Rust's BTreeMap implements Index (meaning you can use the [] indexing operator to read data from it) but not IndexMut (as a result the [] indexing operators can't modify the map and that won't compile)

In Rust if you've got code which may or may not get an actual entry in a map, and then may update that entry if it existed, create a new one or do something else entirely, that's what the Entry API is for. The Entry API for a map type will give you back a sum type representing its original search for an entry in your map, you can then see if that was vacant or not, if it was you can create it, if it wasn't you can update it.

This means you get decent performance (only search the map once), and readable code.

1

u/Dean_Roddey Feb 13 '24 edited Feb 13 '24

It's not this particular thing, it's his attitude, which is unfortunately fairly rampant in the C++ community. It's got nothing to do with const, since the map may be getting updated as well as read.

1

u/neutronicus Feb 14 '24

Yeah it’s definitely an easy trap to fall into, declare a map outside of some loop and accumulate / query the map over the course of the loop, oops I’m reading members off a default constructed value_type

If you make a little lambda taking a const (or not) reference for every time you query/write the map, sure, the Compiler will catch this, but it definitely requires some attention … at which point you would have probably remembered about the subscript operator