Hacker News Re-Imagined

We used C++20 to eliminate a class of runtime bugs

  • 144 points
  • 7 days ago

  • @maccard
  • Created a post

We used C++20 to eliminate a class of runtime bugs


@joatmon-snoo 6 days

Replying to @maccard 🎙

I recently bumped my company's fmtlib version to the one that has compile-time format strings, and it's been such a load off our minds to not have to worry about this anymore. We're reasonably early stage (12 people right now) and even with all of us being reasonably senior, I still found a few callsites where the compile-time format string caught bugs.

(FYI: for anyone holding off on this because of issues with spdlog, enough has been fixed in the latest versions of both that you can upgrade them together now.)

Also worth noting: the UX of fmtlib's compile-time format strings is actually quite interesting. There are error callbacks in the consteval calls that take advantage of the context that compilers show in compilation failures that detail the mistake being made.

(Aside: I'd also like to note that `absl::StrFormat` has supported compile-time format strings for quite a long time. https://abseil.io/docs/cpp/guides/format)

Reply


@LAC-Tech 6 days

Replying to @maccard 🎙

I haven't used C++ in years. I can remember writing C++11 and transitioning to C++14. And also lots and lots of Valgrind :)

What's it like these days? Is tooling pretty much the same - downloading packages from linux, some header only libs, and makefiles? Has the Language Server Protocol made in roads with C++ editors and IDEs?

Reply


@DiabloD3 6 days

Replying to @maccard 🎙

Imagine what happens when Microsoft moves to Rust, it will be a red letter day.

Reply


@maccard 7 days

Replying to @maccard 🎙

I saw this this morning (and also rusts support for fmt strings). Whenever c++ is discussed I often find myself defending modern c++ against people who want to write c++03 because it's simpler. I like this article because it shows how useful modern features really are in practice.

Reply


@kazinator 6 days

Replying to @maccard 🎙

What this is saying to me is that they have untested code paths. All the places where error encountered bad parameters are not actually covered.

It doesn't seem incredibly valuable to know that some error() calls in untested code are well-formed, since that code could be broken. The error() could be a false positive, or the compiler could crash before reaching the error() call due to some bug. Or some of the errors() could be in de facto unreachable code: no test case can cause them to be executed.

If you have a test suite which hits all the error() calls, and if the formatting system is robust to catch bad arguments at run-time, you don't have a problem.

Reply


@jokoon 6 days

Replying to @maccard 🎙

I remember using variadic template to implement a print() function, to print any type of variable, with a variable amount of arguments.

I got help to implement it, it was a bit difficult, but quite nice to have.

Reply


@ducharmdev 6 days

Replying to @maccard 🎙

As someone coming from a web background with almost no systems programming experience, Rust is appealing to me in terms of it's language features and support for a functional style of programming, as well as the documentation and tooling (among other things).

But from the little I've learned about modern C++ and RAII, I wonder how systems engineers will respond to Rust in the long-term. Memory safety seems to be the primary argument in favor of using Rust, but what would be easier - rewriting everything in Rust, or refactoring existing code to take advantage of RAII?

Reply


About Us

site design / logo © 2022 Box Piper