Hacker News new | ask | show | jobs
by okanat 2 days ago
I'm a / used to be a C++ programmer for 10 years. I've been lucky enough to work in a company that aims to adopt Rust. I have been working on Rust projects in the embedded space that runs on real-world devices right now for 3.5 years. Slowly but surely, not going into "rewrite every single thing now in Rust" but one component at a time when the project justifies it.

So, tell me what compiler option disables non-modern C++ code? Is there one that enforces that every single variable including stack ones work like unique_ptrs without paying the price?

How about safety checks in std; is there an opt-out style safety checks where I can ensure that I'm not adding random things to a map with the [] operator, the library checks size of vector when I access elements in non-performance-critical code, anybody can use iterators safely without being able to write code that can change contents of a container?

How about std::thread? Is there an enforcement switch that I can only pass in things that work exactly like trivially_constructible<T>s, unique_ptr<T>s or shared_ptr<mutex<T>>s and nothing else?

Is there a compiler switch that completely goes against https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines... and ensures that no unchecked pointer access happens without explicit approval from the developer?

If those all exist, is there a movement to port many of the core building block libraries to that special compiler including std without being afraid of breaking ABI?

Those are all what Rust developers (who were or still are quite advanced C++ devs, btw, not people who are afraid of using raw ptrs) get freely from the Rust compiler. A lot of "modern" C++ code still contains and, by design, cannot avoid creating unintended and unlimited UB.

1 comments

Enable sanitizers on test suites and fuzzers.

Enable warnings and WError

Use clang tidy and other static analyzers

Actually use a modern compiler and enable the safety features they ship with

Most of these things have solutions that would take years of work in an existing project so it isn't done

And it takes significantly more effort to write good modern C++ code than Rust code

So Rust wins

But I don't like writing Rust code, I do enjoy writing modern C++ code, the tradeoff is modern C++ can be a tooling nightmare... Try shipping a modern stdlib on an old platform, it is truly infuriating, I don't want to be a build systems expert but I need to be to use a safer alternative.

All those things you mention still have way more gaps in them than the rust compiler. They're just not really substitutes (fuzzing and sanitizing do still have their place in rust, though).
My experience is that unless that is pushed by DevOps teams, and being strict about it, no matter how long a build is broken, eventually they get disabled because there is this critical delivery, and then they are never enabled again.

Any language that isn't copy-paste compatible with C (or a subset of it), wins.

That is the biggest issue, old habits and old teaching materials keep working, a plus, and a curse.

> Actually use a modern compiler

This was always a pain point in C++ embedded space, to be fair.