News for hackers. Stuff that matters.
Re:We used C++20 to eliminate a class of runtime bugs by ducharmdev on Friday January 14, 2022 @10:27AM (#29935597)
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?

Re:We used C++20 to eliminate a class of runtime bugs by zwieback on Friday January 14, 2022 @10:39AM (#29935768)
I 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.

Re:We used C++20 to eliminate a class of runtime bugs by cogman10 on Friday January 14, 2022 @10:44AM (#29935824)
I'd argue that RAII is pretty well baked into C++.

For Rust, it's the "Drop" trait that you add onto things which ultimately causes RAII behavior.

You don't need to do it as much with rust as a lot of the reasons for wanting RAII (memory management) are simply handled by the language.

For Rust, it's sort of the "Pit of success" the easy thing to do is the right thing to do. Whereas in C++, it's pretty dang easy to new something up and fail to correctly delete it.

Re:We used C++20 to eliminate a class of runtime bugs by maccard on Friday January 14, 2022 @10:48AM (#29935885)
> Whereas in C++, it's pretty dang easy to new something up and fail to correctly delete it.

This is true, but we shouldn't be encouraging people to reach for new/delete these days; auto Foo = std::make_unique<Foo>(args); does the right thing in a surprisngly large number of cases. It's not the only tool available, but it's a really damn good one.

Re:We used C++20 to eliminate a class of runtime bugs by cogman10 on Friday January 14, 2022 @10:57AM (#29936009)
The "Pit of success" and "Pit of failure" aren't really about what's available in a language, it's about what's easy and natural.

I'd argue that in a language with the `new` keyword, nothing is more easy or natural than invoking it. `auto Foo = std::make_unique<Foo>(args);` is absolutely the right thing to do, but would you really be surprised to find `auto foo = new Foo(args);`?

I absolutely agree unique pointers are the right call. They just aren't necessarily the easy or natural call.

The contrast is rust where all ptrs are, by default, unique ptr and the compiler validates that for you.

Re:We used C++20 to eliminate a class of runtime bugs by HALtheWise on Friday January 14, 2022 @02:47PM (#29939375)
I've thought before about making a "dialect" of C++ that doesn't add any new fundamental capabilities, but simply changes syntax to make the more modern options easy and natural and make old C features verbose and difficult. For example, using "*" to represent unique_ptr and std::legacy_c_ptr<...> to represent "bare" pointers. Ideally, such a dialect could be fully include-compatible with all existing C++ code (although macros could be difficult), similar to how Rust "editions" or go "language versions" work. If C++ compilers offered a "#pragma modern_defaults" would people be interested in using it?
Re:We used C++20 to eliminate a class of runtime bugs by pjmlp on Saturday January 15, 2022 @04:06AM (#29944989)
Not really, one of the reasons people keep using C++ is its legacy.

It is already hard to educate people on modern language features and adoption of static analysers that enforce Core Guidelines.

Who would write the books and teaching materials for #pragma modern_defaults, and ensure it would be accepted into ISO?

Re:We used C++20 to eliminate a class of runtime bugs by jcelerier on Saturday January 15, 2022 @03:16AM (#29944747)
Actually in C++ the vast majority of the time, the right thing to do is

    Foo foo{args};

Which is simpler than the other cases, more correct and more performant. The rare few times dynamic allocation of something that is not an array with new is needed is when you have polymorphic types - but then, the user code generally calls a factory function instead of new.
Re:We used C++20 to eliminate a class of runtime bugs by de_keyboard on Friday January 14, 2022 @02:45PM (#29939356)
Can we agree that the new "best default" way of instantiating is... pretty damn weird?

    auto foo = std::make_unique<Foo>(args);
Think about explaining this to a new C++ developer.

This will feel much more natural to most developers:

    var foo = new Foo(args);
Re:We used C++20 to eliminate a class of runtime bugs by not2b on Friday January 14, 2022 @03:18PM (#29939808)
The "best default" way of instantiating is

    Foo foo(args);
Sometimes an object has to be on the heap as its own separate allocation (rather than on the stack or as a data member), but I notice that some new developers who come to C++ from some other language use the heap way too much.
Re:We used C++20 to eliminate a class of runtime bugs by criddell on Friday January 14, 2022 @03:11PM (#29939694)
> Think about explaining this to a new C++ developer.

How's this:

The code allocates and initializes a new Foo instance with args. By using make_unique you guarantee that the destructor is called when foo goes out of scope for any reason.

Re:We used C++20 to eliminate a class of runtime bugs by leetcrew on Friday January 14, 2022 @11:00AM (#29936061)
in my experience, it's almost inevitable that you end up calling some code you don't control that returns raw pointers. better hope you have access to the source or at least some good docs :)
Re:We used C++20 to eliminate a class of runtime bugs by maccard on Friday January 14, 2022 @11:55AM (#29936901)
Agreed. That problem existed even before unique_ptr though; there are still some APIs that I use that hand you back pointers that you may or may not need to manage yourself. If you do need to, then unique_ptr + custom deleter is the way to go!
Re:We used C++20 to eliminate a class of runtime bugs by pjmlp on Friday January 14, 2022 @02:43PM (#29939330)
If 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.

Re:We used C++20 to eliminate a class of runtime bugs by cogman10 on Friday January 14, 2022 @10:47AM (#29935861)
> rewriting everything in Rust, or refactoring existing code to take advantage of RAII?

Oh, refactoring existing C++ code is easier, hands down.

You can, for the most part, keep your C++ algorithms exactly as is with little rewriting. Rewriting in rust may force you to totally rethink your approach (or use a lot of unsafe, or write really inefficient code).

Re:We used C++20 to eliminate a class of runtime bugs by leetcrew on Friday January 14, 2022 @10:54AM (#29935961)
I'm not sure I fully understand the question. RAII is powerful, but it is just a tool to organize what is still manual resource management. it can help with memory/thread safety, but it doesn't guarantee those things by itself. ex: it's nice that std::vector's memory buffer will be freed when the object goes out of scope, but I can still do something stupid like hold onto to its .data() pointer and try to dereference later.

to answer the more general question, I think most engineers would prefer to refactor a c++ project over rewriting the whole thing in rust, and rightly so. you usually don't want to fuck around with delicate things that currently work in these sorts of projects. perhaps over time you end up with an FFI layer over a thoroughly tested c++ core. then new features can be written in rust or whatever friendly language is popular at the time.

Re:We used C++20 to eliminate a class of runtime bugs by ducharmdev on Friday January 14, 2022 @12:53PM (#29937633)
I appreciate the clarification, my understanding of this stuff is pretty superficial.

Putting it that way, I can see why someone would appreciate that Rust guarantees safety by default, instead requiring you to opt-out explicitly when needed (with `unsafe`), versus the opt-in nature of RAII - for greenfield projects, anyway.

Re:We used C++20 to eliminate a class of runtime bugs by rileymat2 on Friday January 14, 2022 @03:12PM (#29939702)
One quibble one of the best uses for RAII is not memory management but any external resource like db connections or file handles or mutexes.

The built in smart pointers take care of much of memory management. (Although they use RAII internally)

Re:We used C++20 to eliminate a class of runtime bugs by CJefferson on Friday January 14, 2022 @03:44PM (#29940181)
I'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.

Re:We used C++20 to eliminate a class of runtime bugs by pjmlp on Friday January 14, 2022 @02:41PM (#29939302)
RAII was already a thing in C++ compilers for MS-DOS.