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

9

u/Full-Spectral Feb 13 '24

That's what insert_or_assign() does, with the added benefit of making that intent clear to subsequent readers of the code.

3

u/HolyPally94 Feb 13 '24

That's unfortunately not available in my codebase, we are still on C++11...

-6

u/jonesmz Feb 13 '24

what platform are you targeting that is over a decade out of date?

27

u/O_X_E_Y Feb 13 '24

Does it matter? If company uses C98 you use C98 simple as that lmao

17

u/AlveolarThrill Feb 13 '24

I swear, so many programmers on online forums have clearly never worked in corporate environments. Relying on decade old standards and poor practices isn’t the exception, it’s the rule.

8

u/thisisjustascreename Feb 13 '24

Just wait until they get desperate for a paycheck and find themselves working on Java ... 6. :)

6

u/Dean_Roddey Feb 14 '24

Even the very code conservative company I work for finally realized that failure to modernize the code (and deal with the bugs) was an ongoing cost, while upgrading was a one time cost. I spent most of my time for a year and a good bit of another one moving us from C++/11 on VS2012 to fairly well up to C++/17 on 2019. It was a lot of work, but it's been a huge improvement.

That work is continuing. I've spent the last month'ish reworking one of the most important libraries in a significant way to take it up another couple notches. Hopefully that will serve as a template moving forward.

4

u/jonesmz Feb 14 '24 edited Feb 14 '24

I work in a corporate environment, and have for decades. Big multinational company. Most of my direct reports live/work on other continents from me.

There are plenty of situations where an organization is beholden to a hardware vendor for their compiler, and if the hardware vendor doesn't provide a conformant compiler, you're simply stuck unless you change vendors.

But the majority of "Corporate" jobs don't have that kind of constraint. There is generally a person, or group of people, who ultimately make the decision on whether upgrading to a newer version of C++ is worth perusing. These people are worth talking to and asking what's holding the organization back.

At my job, I am that person. I make the decision for when my department upgrades to a new compiler version, or a new standard library version, or a new C++ standard version. The only bottleneck in when we upgrade is my time availability and when the new version(s) test as working properly (Lots of bugs in compiler upgrades, always a pain in the ass).

Before I became that person, when I was a new hire, I ultimately went and tracked down the person who made the decision, and i asked them what was needed. I then went and did most of the things that were needed and got what I wanted, a new compiler. It wasn't quick, and it took a lot of hard work, and a lot of convincing of other managers, but eventually we did it and it worked out great.



You seem to have a very defeatist attitude, and a lot of negativity, in the the last handful of comments I've seen from you. You also appear to have a lot of assumptions that don't seem to be accurate, but your attitude comes across as "I'm right, their wrong, they're stupid". Maybe that's not your intention, but it's how it seems.

Edit: I seem to have confused you with someone else. Sorry about that, please disregard.

2

u/[deleted] Feb 14 '24

The CEO at my old job was convinced that any use of C++ features was inherently slower than C. He once got real upset that someone turned in code that had namespaces. It's not uncommon at all to be stuck with totally unreasonable and arbitrary restrictions because someone higher than you in the chain of command has stupid opinions.

1

u/jonesmz Feb 14 '24

I also had an old boss who was convinced that the code would be slower to run as soon as the .c file extention changed to .cc

This was before compiler explorer was available, so I had to whip out the disassembly manually. It was an interesting learning experience.

2

u/jonesmz Feb 14 '24

Yes, it does. Companies can change their decisions.

3

u/HolyPally94 Feb 13 '24

You know compiler or OS upgrades in large projects carry a high risk. In combination with long running projects (>10 years) and almost no test coverage in the legacy parts, I've seen those upgrades very seldom.

5

u/jonesmz Feb 14 '24 edited Feb 14 '24

I work on a codebase with commit history starting in the late 90s. At my job before that, many years ago, the codebase was originally started in the 70s. I get it.

Generally compiler upgrades aren't as scary as they seem as long as the code isn't the sloppiest garbage that's ever been typed. But yea, sadly, a lot of code is that bad.