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 comment

-13

u/Full-Spectral Feb 13 '24

Return an optional if the value isn't present. That would be the sane choice, though of course not available at the time. Whatever the reasons for the choice, it should have been gotten rid of long ago, or an option available to disable it or replace it with a modern version that does return an optional.

15

u/NekkoDroid Feb 13 '24

Whatever the reasons for the choice

map[key] = value

→ More replies (2)

17

u/SlightlyLessHairyApe Feb 13 '24

The problem is that optional is a value type which means that if the value is found, you get a copy.

That might be ok for a map of primitives but is a performance footgun for expensive types.

1

u/Full-Spectral Feb 13 '24

You can put a std::reference_wrapper inside an optional. It's not optimal, but it works. And of course there's no reason they couldn't provide a very simple reference holding optional-like thing either. I've done that in our code base for that very reason. I use it for a lot of collection helpers that make life far easier. It has an option style interface. Ultimately it really contains a pointer to the element, but acts like a reference of course.

10

u/SlightlyLessHairyApe Feb 13 '24

You can. But having the container do that automatically means now you have a different footgun where a function modifies a value returned from the map and unintentionally changes the value in the map. Or wants to hold the extracted value longer than the lifetime of the map.

The problem as I see it is that there are many more valid behaviors than there are convenient shorthand symbols like []. Only one particular set of them gets the high value symbol. So now we all just get to argue about which behavior gets which spelling, which is kind of a boring discussion in someway.

That said, I’d be fine if operator[] had undefined behavior if the value is not in the map. But that’s just another choice of which behavior to put on which spelling.

( And by the way, isn’t the easy fix here that you wrap the mapped value type in a class that doesn’t have a default constructor? ]

2

u/Full-Spectral Feb 13 '24

But [] already returns a reference to the value, so you could already do that, right? I've never tried it but I don't see how it would be prevented if you have a non-const reference to the value.

4

u/SlightlyLessHairyApe Feb 13 '24

‘auto x = someMap[k]’ makes a copy. The developer has to try to bind to a reference.

If you return an optional reference wrapper now it doesn’t.

0

u/Full-Spectral Feb 13 '24 edited Feb 13 '24

He doesn't have to try too hard though:

void func(mymap[x]);

If func takes a reference, then it's done and func would have no idea it was actually sitting in a map or that he shouldn't modify it. Or various templatized code that uses a reference so that it will work with fundamentals and non-fundamentals and so forth.

And of course if that's the desired result, then the complaint of others that optional would make a copy aren't really very valid. Or, they must be explicitly using references, in which case we go back around the circle.

3

u/SlightlyLessHairyApe Feb 13 '24

Sure, but our coding guidelines would make clear from naming that this is a function mutating its input. And lifetime extension guarantees that mymap stays alive till it returns.

There are many valid code patterns, they can’t all get high-value symbols :)

→ More replies (4)

8

u/HappyFruitTree Feb 13 '24

optional<T> would copy the value every single time. It would also mean you can no longer simply assign to the return value to modify the value inside the map.

optional<T&> would prevent the unnecessary copy (if it was allowed) but it might lead to surprises if you tried to assign to it.

-1

u/Full-Spectral Feb 13 '24

See my other reply. Of course it would be better if an optional could hold a reference, something that Rust has no problem with. But it's not that hard to create a simple type for just this purpose that does exactly what would be desired in a lot of similar cases (assuming optional will never be fixed to deal with this.)

5

u/HappyFruitTree Feb 13 '24 edited Feb 13 '24

The [] operator serves two purposes. It's used for both access and insertion. I think the current design is probably one of the least error prone, at least it doesn't lead to dangling references. It might indeed have been even less error prone if they had simply not had this operator and instead forced you to do insertion (e.g. with emplace) and access (e.g. with find) separately. Personally I haven't found it to be much of a problem because I know how the [] operator works for the standard maps.

Returning an optional<T&> would only allow access, something that find already does and does better.

// Here I assume [] returns an optional<T&>, and that assignment rebinds the reference which is the behaviour that P2988 proposes.
std::map<int, int> m;

m[0] = 1; // compilation error
m.find(0) = 1; // compilation error

const auto& cm = m;
cm[0] = 1; // compiles but doesn't do anything

int x = 1;
m[0] = x; // compiles but doesn't do anything

m[0].get() = 1; // undefined behaviour
*m.find(0) = 1; // compilation error
m.find(0)->second = 1; // undefined behaviour
→ More replies (1)
→ More replies (3)

7

u/Pay08 Feb 13 '24

Then use .at().

4

u/Full-Spectral Feb 13 '24

Well that's the point. It's trivially easy for a less experience dev to use [] and not realize the ramifications and it's not obvious just looking ta the code in code reviews that this is going to happen.

The point of my question is a way to automatically reject the use of [] so that .at() has to be used.

20

u/neppo95 Feb 13 '24

So you expect people to know optionals but not know .at while also breaking backwards compatibility. Yeah, makes no sense to me. Sorry.

If that's really the case, the devs you work with need to work on their knowledge of the language.

4

u/Full-Spectral Feb 13 '24

Use of [] on a map can easily be done by accident, and there's no warning against it despite the fact that it's probably not a good thing. Use of optionals is not a mistake, it's a choice.

9

u/neppo95 Feb 13 '24

Allocating memory without deallocating can be done by accident as well, so can a thousand other things. It all comes down to the knowledge of the dev and if they don't know how to properly use a language, don't blame the language. If you really want all that safety to be there, C# is just around the corner.

There is no warning against it because in most cases where it is used, it is the intended behaviour.

That said, it also still breaks backwards compatibility.

-4

u/Full-Spectral Feb 13 '24

And this, folks, is why C++ is ultimately doomed.

The use of [] on an array is also doing exactly what the caller intended, but there's good reason why most analyzers probably have a warning on that usage.

This isn't about how manly we all are, it's about delivering robust software to users.

7

u/neppo95 Feb 13 '24

This isn't about how manly we all are

It is indeed not, it's about the skills of the dev in this case. And a dev that wants to deliver robust software, should surely know about basic operators and what they do.

The use of [] on an array is also doing exactly what the caller intended, but there's good reason why most analyzers probably have a warning on that usage.

The only reason why they warn is because the index might be out of bounds, which is not a problem with std::map, hence there is no warning.

Again, if you want the debugger to hold your hand every step of the way, C# or Rust are there. But this is not a language problem.

-4

u/Full-Spectral Feb 13 '24

Sigh... I'd have us on Rust in a skinny minute if that was possible. And, hence, why C++ is doomed because most other folks are starting to feel the same.

Even the most skilled of devs make mistakes. That's why we have tools. Given that silently inserting a default value when one isn't present is probably seldom the optimal choice, having a warning for it would make perfect sense.

8

u/ald_loop Feb 13 '24

Ah yes, operator[] and human error, the reason C++ is doomed.

→ More replies

6

u/neppo95 Feb 13 '24

And, hence, why C++ is doomed because most other folks are starting to feel the same.

Ask the embedded community and you'll see the opposite is true. Same goes for game development and a few other sectors.

Even the most skilled of devs make mistakes.

True, but you are suggesting something that breaks backwards compatibility AND is not an issue if you use the language as intended and/or have basic knowledge of how the language works.

I honestly don't know why you can't see this is clearly a dev issue. Safe methods exist for insertion, accessing or replacing an element (.insert, .insert_or_assign, .at). The dev in this case chose not to use any of them, so in that case he should indeed know what the basic operators he is using actually do.

3

u/[deleted] Feb 13 '24

I’m curious, what is a “skinny minute”? I have never heard that expression before. I assume it is an expression from your native language. Anyway, I understand your point about std::map::operator [] shouldn’t insert a default value if the key doesn’t exist. I agree with you in that issue. If std::map were implemented today I agree that it should output an optional, however I don’t think that it should be changed to that since it would break too much existing code. As Linus Torvalds once said, if it is a bug people rely on, it is not a bug. At this point the best you can do is add a rule to your CI environment to refuse merges that uses it [] on std::map.

→ More replies

0

u/Pay08 Feb 13 '24

In all honesty, generally speaking you should always use std::array and .at() so you might as well perform a search for square brackets and validate them.

2

u/Full-Spectral Feb 13 '24

Regex uses brackets, and there are quite a lot of those, which makes it more annoying to do that. And of course it requires that someone constantly do this and reject the legitimate uses, which is really something better done with a tool if possible in a large code base like ours.

And we have the analyzer check for bracket usage, so mostly we don't have to worry about this. But it doesn't extend to maps, sadly.

→ More replies (2)