1. 9
  1. 9

    To get this wrong does take some effort, and in well-crafted code should never happen

    And then proceeds to show an example where the difference between well-crafted and non-well-crafted code is a single character suffix to a string literal.

    But that’s just par for the C/C++ course. Not a day passes where some well-crafted code turned out to not be and at the same time no day passes without a C programmer telling us that their code is fine and the mistakes were made by others

    1. 1

      I don’t think that’s entirely fair. The example is particularly contrived to show the difference (and is something that static analysis will pick up, even if it did pass code review). In practice, std::string_view is used almost entirely for non-captured function arguments. In this usage, it is entirely safe (the caller must own the underlying character storage and if you want to pass a string literal, you’ll just pass a string literal directly and not explicitly construct the string view).