Map: Operator[] Should Be Nodiscard

(quuxplusone.github.io)

35 points | by jandeboevrie 4 days ago

8 comments

  • junon 49 minutes ago
    For the Rust inclined, [[nodiscard]] is #[must_use], if you were confused.

    Anyway, this article illustrates a great reason why C++ is a beautiful mess. You can do almost anything with it, and that comes at a cost. It's the polar opposite ethos of "there should be one clear way to do something" and this sort of thing reminds me why I have replaced all of my systems language needs with Rust at this point, despite having a very long love/hate relationship with both C and C++.

    Totally agree it should be marked as nodiscard, and the reasoning for not doing so is a good example of why other languages are taking over.

    • bayesnet 5 minutes ago
      It’s also worth noting that in rust you don’t need to be as worried about marking a function #[must_use] if there is a valid reason some of the time to discard the value. One can just assign like so `let _ = must_use_fn()` which discards the value and silences the warning. I think this makes the intent more clear than casting to void as TFA discusses.
  • reactordev 1 hour ago
    My pet peeve with c++ is exactly this. Either it’s not wise to call release, or it is (under circumstances) and yet the developer has no idea whether their scenario applies (tip: it doesn’t, 90% of the time).

    The stdlib is so bloated with these “Looks good, but wait” logic bombs.

    I wish someone would just draw a line in the sand and say “No, from here on out, this is how this works and there are no other scenarios in which there needs a work around”. This is why other systems languages are taking off (besides the expressiveness or memory safety bandwagon) is because there are clear instructions in the docs on what this does with examples of how to use it properly.

    Most c++ codebases I’ve seen the last 10 years are decent (a few are superb) and I get that there’s old code out there but at what point do we let old dogs die?

  • j1elo 17 minutes ago
    C++ could try to approach the "stability without stagnation" model.

    Add an opt-in compiler flag --edition='26' which, when used, applies the breaking changes defined for C++26. Then users like Google or others who have been (ab)using some features for their side effects can decide to stay on the older versions.

  • fn-mote 1 hour ago
    Title could be “ugly C++ idioms prevent map::operator[] from being [[nodiscard]]”.

    Many of the uses are in Google’s codebase.

    Overall very technical- interesting if you are a library writer or maybe if you care about long term improvements in your C++’legacy codebase.

  • themafia 28 minutes ago
    Ah, and because this is C++, the standard map having typed template parameters, which could be a non pointer, they're forced to make operator[] have this semantic:

    Returns a reference to the value that is mapped to a key equivalent to key or x respectively, performing an insertion if such key does not already exist.

    Which is a bit of a surprise coming from mostly C and Go.

  • rwmj 1 hour ago
    https://en.cppreference.com/w/cpp/language/attributes/nodisc... .. in case anyone else was wondering. It seems to mean the compiler should warn if you ignore the result except by an explicit cast to void.
    • larusso 1 hour ago
      Thanks. I was wondering what this means practically. So they rolled this back because Google who compile with warnings as errors can’t fix these lines? Must be great to be Google and on all these boards. I on the other hand have to deal constantly with breaking changes left and right because someone decided, among them Google (16KB page tables anyone), that going forward stuff works differently.
  • dooglius 36 minutes ago
    `try_emplace` is not a huge improvement since it overloads the existing keyword "try" to mean something pretty different. Should be `emplace_if_absent`/`insert_if_absent` but changing the API of stdlib would require going through a huge formal process