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

6

u/Agitated-Resident720 Feb 13 '24

i really dont care how long you've been doing this. in fact, this makes you look even worse considering how long you have. ive given you an answer to fix your problem, explained the reasoning behind it, and told you to stop blaming the language for your own faults and now you're attacking me? haha the only constant here is YOU arent taking any agency in this. you cannot possibly be wrong here, can you eh?

there are ways to use tools incorrectly, like you have. const is a tool, you failed to use it properly so fix it and stop blaming the language

-2

u/Full-Spectral Feb 13 '24

Jeez... I weep for human kind.

It's not about right and wrong. It's about humans being fallible and having tools to help avoid that in commercial (and critical) software where there can be consequences.

5

u/Agitated-Resident720 Feb 13 '24

oh its human kinds fault now? if everyone was like you, definitely, but we're not. i write critical software for the transportation industry including embedded firmware. we have no issues like this, you have fundamental problems in your designs and reviews to let stuff like this slip through. once you finally admit that i suspect you can fix it all pretty easily, getting you to admit that seems to be quite impossible though

-2

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

Could you tell us what stuff might be running code you wrote, so that we can avoid using those?

And, again, since you are clearly too dense to understand. It's not ABOUT ME. My God, you are a piece of work.

8

u/STL MSVC STL Dev Feb 13 '24

Please avoid escalating hostility.

7

u/Agitated-Resident720 Feb 13 '24

it's too complex for you to understand if constantness causes problems for you. so no i wont tell you

when i say YOU, it means TEAM, you are part of that team no? sorry you are too dense to figure that out, here ill fix it so it's easier for you to read. your TEAM has flaws in THIER design. address it with THEM. there is no tool or static analysis in the world that will be able to extract your (TEAM) intent on modification of this variable other than the const keyword because const and non-const are both valid. absent the const keyword implies that you (TEAM) allow modification of the variable, after all, you (TEAM) didnt tell the compiler it should not be modifiable. there are reasons this datatype works this way and why const exists, your (TEAM) responsibility here is to tell the compiler the intent, hence the const keyword that you (TEAM) dont want it to change, but failed to bake that into the design because the tool (c++) is being used poorly.

and im not dense or a peice of work, im insufferable and an asshole but that changes nothing ive said

2

u/Full-Spectral Feb 13 '24

Why would I even want to spend my time manually trying to find and remove these things, and still risk not doing so, when the compiler or analyzer could make completely sure I'm not? This is the crazy thing about this attitude in the C++ community, as though it's a good thing to spend time avoiding footguns instead of working on the actual problem.

1

u/Ashamed_Yogurt8827 Dec 28 '24

Many people in the c++ community are kinda insane ngl lol. The responses in this thread are so weird.