• (nodebb)

    I'll be honest, what bugs me most is the Hungarian notation on local variables. But I'm long established as a Hungarian notation hater.

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

  • RLB (unregistered)

    The final return was probably added to keep a particularly brain-dead linter happy. Or ditto code reviewer.

  • (nodebb)

    I kinda think TRWTF is

    or if the key doesn't exist inserts a default-constructed instance of whatever the map contains.

    I understand that it has the advantage of never throwing an exception, but that's still a rather unexpected side effect.

  • (nodebb) in reply to Dragnslcr

    TBF, it is C++. The proposal says "this is noexcept" and the only option the implementers have is to do something dumb.

  • noOne (unregistered) in reply to colejohnson66

    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()

  • (nodebb)

    Having a function called thingyExists which returns a bool, but has side effects of changing the underlaying data, would seem to be pretty stupid.

  • (nodebb) in reply to noOne

    it's like this on purpose, as shortcut

    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 the some_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.)

  • f222 (unregistered) in reply to Dragnslcr

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

  • Vault_Dweller (unregistered) in reply to Steve_The_Cynic

    Just because it's an old WTF, doesn't make it any less of a WTF.

  • (nodebb)

    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.

  • (nodebb) in reply to Vault_Dweller

    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.

  • (nodebb) in reply to Steve_The_Cynic

    Still beats iterating the array, no?

  • Clive (unregistered)

    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?

  • (nodebb) in reply to Bananafish

    Still beats iterating the array, no?

    Absolutely.

  • (nodebb)

    One argument against adding a std::map::containsfunction is that std::map::count basically does the same job and has the same performance.

    does it have the same performance? Surely count has to visit every element of the collection whereas contains can stop after it finds one match.

  • Duke of New York (unregistered)

    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.

  • (nodebb)

    Probably because the find function is confusing to new users. Although the array index override is also confusing.

  • richarson (unregistered)

    Hey, how is the counterfeit post not held for moderation? (and still present)

  • Klimax (unregistered) in reply to jeremypnet

    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)

  • Duke of New York (unregistered) in reply to davethepirate

    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.

  • FTB (unregistered) in reply to jeremypnet
    Comment held for moderation.
  • FTB (unregistered)
    Comment held for moderation.
  • no (unregistered) in reply to Clive
    Comment held for moderation.

Leave a comment on “An Exert Operation”

Log In or post as a guest

Replying to comment #:

« Return to Article