7 ms·
We used C++20 to eliminate a class of runtime bugs
- joatmon-snoo 5y agoI 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 https://abseil.io/docs/cpp/guides/format)
- LAC-Tech 5y agoI 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?
- ncmncm 5y agoC++20 is as big an advance as 11 was, and is taking as long to penetrate. Editors all know C++ now. Generic lambdas in C++14 made coding fun. Template metaprogramming is hardly ever needed or useful anymore. Builds got faster with module support (essentially standardized precompiled headers), and also ccache and ninja. As Concepts penetrate, error messages get radically better. Valgrind is increasingly unnecessary, as use of op new vanishes.
- flumpcakes 5y agoOne of the most interesting changes to me is how much they are pushing metaprogramming into the language itself _without_ a lot of the traditional annotations that went with TMP before. For example, annotating a parameter with 'auto' instead of the typename T preamble.
- ncmncm 5y agoTemplate metaprogramming was always known as a hack, no more so than among people obliged to do it. Most of the things it was used for are now done with core language facilities designed for the job, and coded in compiled C++ instead of interpreted template flailing, so fast.
- kazinator 5y agoWhat 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.
- maccard 5y agoI 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.
- rixed 5y agoComplexity can solve issues but can also bring new ones. For instance, I've been bitten recently by the new "universal" syntax to write variable definitions with only curly braces. #include <iostream> #include <string> #include <vector> using namespace std; int main() { // Forty-two things: vector<string> v1 { 42, "foo" }; vector<int> v2 { 42, 1 }; vector<char> v3 { 42, 'x' }; cout << v1.size() << " " << v2.size() << " " << v3.size() << endl; return 0; } This prints, of course: > 42 2 2
- maccard 5y agouniversal initialization is the worst feature added to modern c++; these changes are unexcusable IMO.
- edflsafoiewq 5y agoThe committee loves overloading. IMO they overuse it. I think once you start thinking "this overload only participates in resolution if..." you should start considering different functions.
- staticassertion 5y agoI feel like there was success with overloading `<<`, which is weird but very appealing for students IME, and suddenly overloading everything was on the table.
- tialaramex 5y agoOverloading << is part of Stroustrup's iostreams toy and thus enjoyed the advantage that the language's creator was advocating it from the outset (it pre-dates the C++ standard library). I doubt that anything so ugly and dangerous would be standardised even in C++ today. Because this is actually still a shift operator, it retains all the precedence of the shift operator even when tired humans think of it as as a clever new stream out operator. So if you're in a situation where shift has precedence, the operator fires, and too bad if that wasn't what you'd intended nor what makes sense to a human reading the code. Oops. Some languages let you mint new operators (from some limited combinations of symbols) or allow infix function calls, or by some other means provide for you to nicely extend syntax here, but the overload of unrelated operators was IMNSHO an ongoing disaster whose popularity with programmers is unexplained, like C's decision to treat an array as just the pointer to the front of the array. But I'm a cranky old man, I also don't like the fact that Rust's + concatenates Strings (this isn't inherent in the language since String isn't a built-in type, in Linux kernel Rust nothing like this exists, because Linus would throw a fit, but in the out-of-box runtime environment Strings implement the Add operator as a concatenate operation).
- jokoon 5y agoI 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.
- DiabloD3 5y agoImagine what happens when Microsoft moves to Rust, it will be a red letter day.
- muricula 5y agoMSRC is pushing rust internally: https://msrc-blog.microsoft.com/2019/11/07/using-rust-in-windows/ https://msrc-blog.microsoft.com/2019/11/07/using-rust-in-win...
- flumpcakes 5y agoMicrosoft have done a lot of working attempting to make every windows C/C++ API available for use in Rust. This task is/was too big to do by hand (and sensitive to changes) so they have automated it. See: https://github.com/microsoft/windows-rs https://github.com/microsoft/windows-rs
- pjmlp 5y agoThere is some ongoing Rust adoption on Azure and IoT, but I doubt WinDev C++/COM bastion will ever allow anything else, given how they manage to keep it going over the years.
- scoutt 5y agoWell... a "Microsoft Visual Rust" (integrated with Visual Studio) could be all the motivation I'm looking for to finally learn, mmmm, not learn but to finally put Rust at serious use. With everything integrated like, you know (and this may sound super crazy) graphically-mouse-operated "create new project", "right click -> add new file to project", resource editor with "create a window with a button, double-click the button and generate a function to handle the event"-sort of thing. F7 to build, F5 to debug. More or less the things we are used to since the past 30 years or so!!!
- formerly_proven 5y agoMaybe they can even recreate that amazing Visual Studio project settings dialog for Rust!
- ducharmdev 5y agoAs 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?
- pjmlp 5y agoRAII was already a thing in C++ compilers for MS-DOS.
- CJefferson 5y agoI've been using RAII in C++ since 2002 or so -- so it's not a new thing to do. Personally, I maintain quite a lot of open source C++ code -- I'm not rewriting that in Rust, but I'm writing new projects in Rust.
- zwieback 5y agoI used to be a 100% C++ programmer in the olden days but over the years C# and Python have pushed my C++ percentage down to maybe 10% super time critical code and some embedded. RAII is the one thing I really really miss from my C++ days. From what little I know about Rust it seems to promise a more "baked-in" version of RAII so I'm all for it. Haven't found the time to experiment but looking forward to eventually dipping my feet in.
- pjmlp 5y agoIf you are using a relatively recent version of C#, you can get quite close to it, specially now that using can make use of structural typing. And with help of Roslyn, any type with a "destructor" (aka Dispose) that gets used without a using declaration can be turned into a compiler error.
- cogman10 5y ago