- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Edit Admin
If you're in a situation where, for whatever reason, you have AHN (wart indicates flavour rather than language type), you should use it on all relevant variables. the
l_
prefix here does not indicate flavour, but "locality" (as opposed tom_
for member variables). There is, however, actual SHN in use as well,p
for pointer.But if you have a sufficiently-expressive type system for variables (hint: C++ does, indeed, have a sufficiently-expressive type system), you should avoid even AHN.
Admin
The final return was probably added to keep a particularly brain-dead linter happy. Or ditto code reviewer.
Edit Admin
I kinda think TRWTF is
I understand that it has the advantage of never throwing an exception, but that's still a rather unexpected side effect.
Edit Admin
TBF, it is C++. The proposal says "this is noexcept" and the only option the implementers have is to do something dumb.
Admin
it's not noexcept, constructors can throw or bad alloc or etc. it's like this on purpose, as shortcut, I would guess. If one doesn't want this feature, they can use .at()
Edit Admin
Having a function called thingyExists which returns a bool, but has side effects of changing the underlaying data, would seem to be pretty stupid.
Edit Admin
It's like this to make
some_variable = some_map[some_key];
do something that looks like an array access in a way that's, well, like an array access. Either it creates the map entry (which is occasionally useful, especially for e.g.some_map[some_key]++;
where there's no need to explicitly add the entry as zero before the first time you encounter it), or it explicitly returns a default-constructed value without messing with the creation, but that's less useful because of thesome_map[some_key]++;
thing.And re: the comment about an unexpected side effect, well, sure, but it's been like that for more than 25 years. (Yes, it's like that in C++98.)
Admin
In the containers, when access by index is available, the C++ standard library usually has one method with bound checking one without.
If you chose to use the method without bound checking you get what you ask for...
I personally would have prefer to have an undefined behaviour to be consistent with the non-bound checking method on the std::vector. But apparently UB is bad for many people... 😅
Admin
Just because it's an old WTF, doesn't make it any less of a WTF.
Edit Admin
some_map[some_key] = some_value;
wouldn't be able to insert if it didn't construct a default value.[]
has to return a reference in order for you to be able to assign to it and have that assignment affect the map, and references can't be null so they have to point to an actual value.It's still a WTF, but the alternative is a WTF too.
Edit Admin
No, but it's also a well-known (among C++ists) issue, and after more than 25 years, nobody who touches C++ even semi-regularly should be surprised by it.
Edit Admin
Still beats iterating the array, no?
Admin
I'm confused :-( There's talk of inserts, but the code I can see is an "erase" call - is that really an insert, or what?
Edit Admin
Absolutely.
Edit Admin
does it have the same performance? Surely
count
has to visit every element of the collection whereascontains
can stop after it finds one match.Admin
There was an ad that went around recently where a guy says "I could lose weight by eating salads, but I want another way." I look at complaints about using map::find() to test a key the same way. Just eat the d— salads.
Edit Admin
Probably because the find function is confusing to new users. Although the array index override is also confusing.
Admin
Hey, how is the counterfeit post not held for moderation? (and still present)
Admin
For std::map? Of course it does. (By definition…) For (unordered)multimap? It will do distance, but it is very fast. (Difference is very small)
Admin
Much is confusing to new users. The solution is for them to learn, and the way to make it easier is to have fewer things to learn, a path that C++ abandoned long ago.