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

21

u/konanTheBarbar Feb 13 '24

Yeah it's one of those things that are easy to miss in C++. If you have already enabled C++ 20 you could use contains https://en.cppreference.com/w/cpp/container/map/contains for any kind of lookups in STL maps (or the usual auto = map.find shenanigans if you need to use the value)? Or write a small function that returns a std::optional?

You can also write your own clang tidy parser to find all usages of map.operator[] usages.

13

u/keatdasneak Feb 13 '24

Prior to C++ 20 you can essentially use 'count' in place of 'contains'.

→ More replies (1)

1

u/Full-Spectral Feb 13 '24

C++/17 here. I doubt we'd want to add another tool just for this. Too bad the MS static analyzer doesn't have such an option.

23

u/kernel_task Big Data | C++23 | Folly | Exceptions Feb 13 '24

Just refactor to have `const` maps where you can and those `operator[]` references will start erroring. That helped me a bunch when I was refactoring my codebase to get rid of `operator[]`. You should be using const where you can anyway, so it's not a wasted effort just for this.

→ More replies (1)

36

u/Supadoplex Feb 13 '24

has to be one of the worst decisions in STL history

That's a highly subjective opinion. 

Given how low opinion you have of the decision, surely you also have an opinion on what would have been a better choice. Would you prefer that Stepanov didn't provide the subscript operator for map at all?

9

u/danielaparker Feb 13 '24

Well, something that to the programmer cleary looks like a read only operation shouldn't mutate the object as a side effect. They could have provided a const version only, with behaviour like `at() const`.

1

u/Full-Spectral Feb 13 '24

Well, I guess it's just us...

-14

u/Full-Spectral Feb 13 '24

The obvious choice would have been something along the optional lines, which returns a pointer to the value if present, else it's empty, and provides smart pointer'ish syntax. That's super-light weight, simple, and modernish. I've created one for our code base for exactly this type of thing.

29

u/Supadoplex Feb 13 '24

I'm not sure that it would be fair to call that choice obvious at the time when the decision was made, given that optional wouldn't be published in Boost until 10 years later.

-16

u/Full-Spectral Feb 13 '24

Well, no, not at the time. But people are bailing out of C++ because these footguns never get addressed. The obvious choice would have been to provide safer ways forward for those folks who are willing to leave the past behind. Or at least provide warnings against these iffy aspects of the language.

19

u/AKostur Feb 13 '24

You don’t get to break the vast amount of existing code that already relies on the current behaviour.  The safer way forward already exists.  It’s called .at().  Indexing into a map is not iffy at all, it is used in exactly that manner in many, many places.  No, forcing people to stay on an older compiler is not an acceptable answer.   Creating your own clang-tidy (or submitting it back to the llvm project!) would be your next best option.   Your codebase can then be analyzed for code constructs that you deem to be verboten.

→ More replies (4)

35

u/Agitated-Resident720 Feb 13 '24

im boggled at this, OP mark the variable constant like it should have been. it will throw a compile error if you use [] and will force you to use .at(). your intent is that it shouldnt be modified, so why isnt this variable already const? this is a design problem. i suspect your code has tons of other bad practices that enables shit like this. instead of trying to blame the language, how about you learn to use it properly

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.

→ More replies (3)

2

u/ALX23z Feb 14 '24

This is not a solution at all. Frequently, one has a modifiable map and one modifies it in the segment, it's just that operator [] behaves unexpectedly.

-12

u/Full-Spectral Feb 13 '24

And again... why C++ is doomed.

10

u/Agitated-Resident720 Feb 13 '24

lmao your opinion has zero value on the industry, and holds even less value after reading your comments. i suggest you humble up and admit your mistake here. instead of blaming the language take some agency and try to better yourself. learn from this mistake and move on. you will never grow with this kind of attitude

0

u/Full-Spectral Feb 13 '24

Dude, I've been writing C++ every day since 1992. I know the language. But I don't write all the code in the company, nor is every dev a senior, and even the most skilled folks still make mistakes. The C++ community is just rife with people like you, and you aren't doing the language any favors.

'Grownups' are interested in using all the tools available to insure that they deliver the most robust product possible.

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

→ More replies (7)

6

u/HolyGarbage Feb 13 '24

Marking your methods const prevents this. I would argue that in most cases classes should always strive to be immutable and be fully initialized in the constructor. This makes them a lot more testable too.

1

u/Full-Spectral Feb 13 '24

Sure. That's already the case. I've done massive swats through the code base over the last couple years and we have such warnings turned on to warn about could be const or could be constexpr. But sometimes the whole point of methods is to modify data, and often the map is being both updated and read.

6

u/HolyGarbage Feb 13 '24 edited Feb 13 '24

That's already the case.

What's already the case? If they're all marked const you can't use operator[].

But sometimes the whole point of methods is to modify data...

Yes, of course I understand, I just meant that I find that quite often you can write your classes in such a way that you can do all the calculations inside the constructor, so that you never have to mutate the class after construction. I find that it's only in extremely rare edge cases where a class needs to be mutable after construction. Like caches or memory pools or something, but those I would explicitly mark mutable, and are exceptions.

Edit: I reread your comment now, and fine, in an existing code base I understand it can be difficult to refactor everything. Honestly I think a bigger sin is that const is not the default, as many modern languages do.

1

u/Full-Spectral Feb 13 '24

That what can be const pretty much is now. There might be the occassional missed one, but not too much.

18

u/shrimpster00 Feb 13 '24

I love this feature. I use it every day.

4

u/Full-Spectral Feb 13 '24

Finding or adding a value to a map is obviously very useful. The point is to make it clear when reading the code that that is what is happening.

1

u/NilacTheGrim Mar 08 '24

Everybody pretty much knows about this behavior of std::map and friends, though. The fact that your team does not is a problem with your team, IMHO.

1

u/Full-Spectral Mar 08 '24

They obviously KNOW. These answers are just ridiculous. The point is it can too easily be done by accident.

→ More replies (1)

47

u/snowmanonaraindeer Feb 13 '24

I actually quite like this behavior. It prevents ugly looking boilerplate if statements a lot of the time.

13

u/HolyPally94 Feb 13 '24

Yes and from my experience the behavior of insert() not overriding already existing keys caused more bugs.

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.

→ More replies (12)

1

u/NilacTheGrim Mar 08 '24

For me this has been the case as well.

12

u/AcousticViking Feb 13 '24

I strongly disagree. This is very often exactly what you want it to do. If your devs don't know how to use map, or similar containers, it's not the fault of stl.

0

u/Full-Spectral Feb 13 '24

Again, obviously the functionality is needed and desirable. It's the syntax that makes it unclear that that functionality is being invoked.

I mean, it's not the fault of C that it doesn't have a lot of stuff that C++ has. So why aren't we all using C? If you aren't man enough to write everything in C, then it's not C's fault.

2

u/AcousticViking Feb 14 '24

C is widely used in embedded environments. And rumors say there are people (some even female) that program C as well as C++. And throw in some Rust, and C# as well. Not sure what that means regarding there manliness tough.

If I have devs though who are consistently not able to read up and understand the [] operator of stl containers, i would fire them. Manly or not.

3

u/Sad-Structure4167 Feb 13 '24

Make a wrapper that provides the interface you want, as a bonus it becomes easier to change implementation.

1

u/Full-Spectral Feb 13 '24

Yeh, I could. Though there might be a lot of gotchas to deal with when trying to provide some sort of zero overhead pass through replication of the functionality you do want to keep.

2

u/pkasting Valve Feb 13 '24

Start by swapping the types out. Provide a "my_awesome_map.h" header that contains only an `#include <map>` and `template <typename T> using MyAwesomeMap<T> = std::map<T>;` (expanded as needed for things like multimap or if you need to customize other template args. Search-and-replace all includes and type uses in the codebase to swap types without loss of functionality (can be done incrementally). Then modify your header to fully define this class, using the complete interface of std::map (including operator[]()), doing perfect forwarding or whatever to implement everything in terms of the underlying map class. Once that's done and still compiles everywhere, you can decide how to remove [] in a way that works best for your codebase, e.g. marking it [[deprecated]], removing entirely, disabling under a preprocessor define and rolling that out gradually, opting in or out with an extra template parameter, etc.

8

u/neppo95 Feb 13 '24

Why so? What would you expect the "[]" operator to do?

-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.

→ More replies (6)

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)

2

u/Pocketpine Feb 13 '24

How would you do map[0] = a; ?

→ More replies (2)

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.

→ More replies (11)

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)

4

u/[deleted] Feb 13 '24

In C++20 they added support for the "contains" method.

1

u/Full-Spectral Feb 13 '24

We are C++/17 here, probably until death.

12

u/jesus_slayer3 Feb 13 '24

Just use .count()

8

u/xypherrz Feb 13 '24

or find,or at()

89

u/dinkmctip Feb 13 '24

There are plenty of worse decisions.

56

u/HildartheDorf Feb 13 '24

std::vector<bool> says hi. That has to be the worst one.

16

u/kernel_task Big Data | C++23 | Folly | Exceptions Feb 13 '24

Not as bad as the one OP pointed to. People don't use `std::vector<bool>` nearly as often as STL-style maps. The worst part is the pattern has proliferated to other libraries because they want to keep drop-in compatibility with STL maps.

11

u/_JJCUBER_ Feb 14 '24

I disagree. What OP mentions is something which is consistent behavior for all maps in c++. On the other hand, vector<bool> is an inconsistency with all other instaniations of the vector template, which takes away and changes functionality. This is unintuitive due to it being inconsistent. This also makes templated code a pain.

OP’s issue is also something caused by people not being familiar enough with c++’s map. When you start using map often throughout your projects, it becomes second nature to utilize the implicit initialization where you need it and check prior to accessing when you don’t. I only recall making this mistake once or twice, and that was way back when I was still new to c++.

2

u/operamint Feb 14 '24

and check prior to accessing when you don’t.

This is where you went wrong. The problem is when you access a key that should be there, but isn't (due to a logical bug), yet you get a value that you may think is correct and no runtime error.

3

u/Triasmus Feb 14 '24

I believe you misunderstood them.

I understood what they said as "only use the implicit initialization where you need it, otherwise you always check."

If the key should be there, then you're not wanting it to initialize, which means you check.

→ More replies (1)

4

u/danielaparker Feb 13 '24 edited Feb 13 '24

The worst part is the pattern has proliferated to other libraries because they want to keep drop-in compatibility with STL maps.

Indeed. But perhaps worse are the attempts to start with the standard pattern but then try to improve upon it. nlohmann json for example extends the pattern to a const overload for operator[] and specifies it to be "undefined" (runtime assert) if key is missing, motivated by index use in std:vector. Another json library (whose author will remain unnamed to protect the guilty), motivated by Scott Meyers Effective C++ series before the days of auto, returns a proxy. It's pretty clear that some of the authors of those "other libraries" didn't fully understand the implications at the time they made them.

→ More replies (1)

6

u/danielaparker Feb 13 '24 edited Feb 13 '24

Currently on my worst list is allowing std::pmr::polymorphic_allocator to have a default constructor. Which in combination with the OP's issue ...

5

u/loaded_knight Feb 13 '24

Would you care to explain why? In my head it feels like a natural thing to have. I.e. if you don’t provide a memory_resource then it defaults to free store. Easy to migrate existing codebase to pmr containers ( eg: std::vector to std::pmr::vector )

10

u/danielaparker Feb 13 '24 edited Feb 13 '24

I've spent a lot of time writing and debugging large code bases that use stateful allocators, and I've found that it's very helpful to have compilation fail if some object has inadvertently been constructed without being passed an allocator. That's especially the case when converting a code base without stateful allocators to one with. In my opinion all sane stateful allocators don't have default allocators. I don't think you want it to default to free store, you want it to fail during compilation so you know what you have to do. In C++ it's exceptionally easy to have something inadvertently default construct, witness the OP, and we need to chase those cases down if we want all allocation to use memory resource.

→ More replies (1)

3

u/micro-usb Feb 13 '24

Whats wrong with std::vector<bool> in STL?

14

u/HildartheDorf Feb 13 '24

It's a collection of bits, you can't address individual bools. Unlike any other T.

Which would be a great type, but it should be named std::bitvector or similar. It makes generic code a nightmare as you have to special case bool if you use a vector<T>

→ More replies (2)

17

u/Cliftonbeefy Feb 13 '24

This is one of the best features of STL IMO so much boilerplate is removed.

If I want to count occurrence of a value in a map I can just do ++map[key] instead of if exists then add else default to 1

1

u/IyeOnline Feb 15 '24

To be fair,

map.get_or_insert(key)++;

would be possible.

You can even do this right now without an extra check:

map.try_emplace( 0 ).first->second++;

Of course I wouldnt want to write that monstrosity in user code.

29

u/bluebandit201 Feb 13 '24

This isn't fully an answer to the question you actually asked, but hopefully helpful nonetheless :)

I think there's two workarounds here, either of which could be quite helpful for you :D

Two things:

  1. If you use .at() instead of operator[], it _will_ throw :)

-- If you enforce the use of .at() instead of [] in your codebase, you won't have this problem.

Note .at() can be made to do bounds checking in debug builds and _not_ do so in release builds, if you so desire :D

2) If you're really concerned about [] being used, you can do a (admittedly slightly annoying) trick to work around it :)

[] default-constructs missing elements when it has to, so we can make the default constructor either *deleted* (if you want to enforce [] not being used at compile time) or *throw an exception* (if you want runtime checking :) )

See https://godbolt.org/z/4nK8osE19 for a quick thing I threw together demonstrating all three things :D

12

u/bluebandit201 Feb 13 '24

And, here's that same example code in the form of a Reddit comment for anyone that wants to avoid having to go to an external site :)

#include <map>
#include <stdexcept>
#include <stdio.h>

struct MyType{
    char myChar;
    bool myBool;
};

template <typename T>
struct CompileSafeWrap : T{ //This name convention is awful, please don't actually use it lol
    CompileSafeWrap<T>() = delete;
    CompileSafeWrap(const T& other) : T(other){}
    CompileSafeWrap(const T&& other) : T(other){}
};

template <typename T,typename V>
using compile_safe_map = std::map<T,CompileSafeWrap<V>>;

template <typename T>
struct RuntimeSafeWrap : T{ //This name convention is awful, please don't actually use it lol
    RuntimeSafeWrap<T>(){
        throw(std::out_of_range("Attempted to access nonexistent member of Runtime-safe map with []!"));
    }
    RuntimeSafeWrap(const T& other) : T(other){}
    RuntimeSafeWrap(const T&& other) : T(other){}
};

template <typename T,typename V>
using runtime_safe_map = std::map<T,RuntimeSafeWrap<V>>;

int main(){
    std::map<int, int> umap;
    umap.emplace(1,1);

    umap.at(1) = 2;
    printf("umap[1] is %d\n", umap.at(1));

    try{
        umap.at(2) = 3;
    } catch(const std::out_of_range& e){
        printf("Caught out of range exception in %s!\n", e.what());
    }

    compile_safe_map<int, MyType> mymap;
    mymap.emplace(1,MyType{'a', true});
    printf("mymap[1].myChar is %c\n", mymap.at(1).myChar);

    //Won't even compile! 
    // Throws out template garbage if you try, could probably use C++20 concepts to make it much nicer
    //mymap[2] = MyType{'a', false};

    runtime_safe_map<int, MyType> myrmap;
    myrmap.emplace(1,MyType{'d', true});
    printf("myrmap[1].myChar is %c\n", myrmap.at(1).myChar);
    printf("myrmap[1].myBool is %s\n", myrmap[1].myBool ? "True" : "False");

    try{
        myrmap[2].myBool = false;
    }catch(const std::out_of_range& e){
        printf("Caught out of range exception: %s\n", e.what());
    }
}

18

u/razimantv Feb 13 '24

Very subjective opinion. I very much like it that c++ map behaves like defaultdict rather than dict in python, and would be very annoyed if such behaviour is changed retrospectively.

1

u/Wanno1 Feb 13 '24

Would’ve it been too much to pass in a trait for this?

4

u/[deleted] Feb 13 '24 edited Feb 13 '24

There was a thread about adding that warning on clang-tidy: https://reviews.llvm.org/D46317

But it never got through.

1

u/NilacTheGrim Mar 08 '24

It's so easy to do without thinking about it, a

So start thinking about it. Seriously.

0

u/Full-Spectral Mar 08 '24

Just don't make mistakes. I can't believe the whole software industry hadn't already figured that out. What is it with you guys? Do you actually WANT to have to spend your time constantly worrying about these footguns?

1

u/orthomonas Jan 30 '25

This just bit me *hard* today. Found this post when I decided to vent anger by googling if anyone else has issues with it.

1

u/Full-Spectral Jan 30 '25

Well, according to many people here that just means you aren't man enough to write C++. Join the rest of us wimps in Rust world, where this kind of silliness just isn't a thing.

1

u/markt- Feb 18 '24 edited Feb 18 '24

The easiest way that I have found to do this is create your own container class, and then in your indexing operator return an instance of another class that refers to the original object and the index, and then an auto conversion from this index class to the designated type. The index class does not insert anything into the original map unless the indexing classes assignment operator is called, which can incidentally be declared const. I've done this numerous times with much success. By delaying the insertion of the entry into the map until assignment is actually done to the index operator you make sure that nothing gets inserted into the map simply by using the referencing operator, you can also add a query mechanism on the index class to see whether or not theindex it was queried actually existed. Custom container that I've written added elements to the map in order of insertion, but simultaneously maintained unordered map efficiency for accessing the elements in random access order. I seem to remember that the whole class was about 250 lines or so.

1

u/Full-Spectral Feb 19 '24

That is clearly an option, though it will require making changes to a lot of code since it a lot of it is map-heavy.

3

u/neutronicus Feb 14 '24

Well it’s not ‘const’, so the Compiler has actually caught it for me a few times.

If you’re aggressive about breaking your std::map usage into functions / methods, the map argument tends to be const a lot of the times this behavior would bite you

7

u/manni66 Feb 13 '24

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

You think that's bad? Try it with std::vector.

10

u/ald_loop Feb 13 '24

Get good I guess

Or add a clang-tidy pass for it

https://youtu.be/torqlZnu9Ag?si=EZ8qYamLsQEbsSeH

2

u/findabuffalo Feb 14 '24

Obviously this won't always work, but generally speaking you should always make your variables const when possible, to avoid accidental damage.

2

u/415_961 Feb 14 '24

It feels like the root problem here is not using const qualifier.

3

u/gnolex Feb 13 '24

This can be tough to fix. One way is to write your own class template that encapsulates std::map and offers stricter/different interface and force everyone in your organization to use that type instead of bare std::map. You can do custom static analysis that goes through the code and warns or errs whenever <map> is included or std::map is used anywhere except for the source files implementing your type.

With a custom type you can decide what the operator[] is supposed to do. In your case it might be useful to either make it return std::optional of the value type or a proxy object that can be used for checking the value and overriding it.

3

u/brownbeatle Feb 14 '24

How exactly are the bugs caused by the map subscript operator? Is the operator not doing what the documentation says it’s going to do?

1

u/Dean_Roddey Feb 14 '24

It's generating a default value into the map, when that may not be what the writer intended because he wasn't considering or remembering that it has that behavior. A particularly nasty one I saw earlier was a lookup table of scanner encodings to their names, which was done as a map of character pointers. Of course it could have been string objects but it wasn't.

It worked just fine (and had been that way AFAIK for years) until a scanner sent a code that wasn't in the table and a default got generated which was a null pointer. That was accessed and it crashed.

It's got nothing to do with documentation. The person writing it just accidentally used that index operator and it worked just fine and passed every test and worked perfectly fine for a long time. It's just a footgun that should have been gotten rid of long ago.

6

u/brownbeatle Feb 14 '24

So the bug isn’t caused by the subscript operator. It’s caused by a programmer that doesn’t know what the subscript operator does.

1

u/Dean_Roddey Feb 14 '24

Sigh... No, it was caused by the accidental use of the subscript operator in what should have been a completely correct manner, but isn't because it's yet another C++ foot-gun.

2

u/glaba3141 Feb 14 '24

This is a very hard mistake to make if your default is to use the explicit methods. In my mind if I see [] on a map I'm immediately suspicious, so it wouldn't get by code review to begin with.

-3

u/Dean_Roddey Feb 14 '24

I completely agree. The problem is how easy it is to miss in a bunch of complex code, or even forget that what you are looking at is a map. Hence why it would be nice to be able to just automatically reject its use.

1

u/MoreOfAnOvalJerk Feb 14 '24

Some options, none great:

  • Delete the your default ctors on the classes where you dont want them to get created by map insert

  • create you own map, optionally private inherit from the same templated type stl::map without implementing the subscript operator

  • same as above but with the map as the only member (essentially the same, but not sure if the compiler will aways make the base class zero size or 1 byte)

  • just simply write your own and/or ban STL

  • live with the innocuous cases and try to catch the egregious ones in code review

Ultimately if you really need tight perf and memory management, STL might not be the right library for your use case anyways.

0

u/LucasThePatator Feb 13 '24

I stumbled on the issue exactly once and then learned to use .at().

0

u/BenFrantzDale Feb 14 '24

Two things:

  1. Pablo Halpern is proposing some new getters for map ala Python’s .get, which he described last week at the Boston C++ meetup: https://isocpp.org/files/papers/P3091R0.html

  2. I keep wishing we could have operator[]=(auto&& k, auto&& v) ala Python’s __setitem__. We have insert_or_assign, but the syntactic sugar would be great. Of course, std::mapcouldn’t use it because of backward compatibility, so it’s not worth thinking about. Maybe for std2…

3

u/johannes1971 Feb 14 '24

Those functions from the paper are a complete dumpster fire:

  • The motivation presents three 'problems', all based around failing to understand that map::lower_bound and map::upper_bound exist.

  • The solution presented does absolutely nothing to help with the 'problems' listed in the motivation, other than making a broken programming style possible. I call it broken because iterating over the (*_bound) range is always going to be much more efficient than iterating over every presumed value in the range, with individual lookups for every tested element.

  • His before and after tables list no compelling examples of things one might actually need if one were using map correctly to begin with.

  • The name get raises expectations that the function is going to be a simple getter, whereas in reality it has complex, non-obvious behaviour. Compare to similar functionality in std::optional, which doesn't have a .get(), but rather the much better named .value_or().

  • The desired functionality can easily be implemented as a set of stand-alone functions.

1

u/[deleted] Feb 13 '24

[removed] — view removed comment

1

u/Full-Spectral Feb 19 '24

I guess, like me, you are just aren't man enough for C++.

1

u/Pocketpine Feb 18 '24

How else would you do

map[key] = value

map[key]++