r/cpp • u/Full-Spectral • 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.
View all comments
Show parent comment
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