News for hackers. Stuff that matters.
Re:We need to seriously think about what to do with C++ modules by holowoodman on Sunday August 31, 2025 @05:40PM (#45087356)
Why bother? The world seems to have moved on to Rust, C++ is only for legacy maintenance stuff anymore.
Re:We need to seriously think about what to do with C++ modules by okanat on Sunday August 31, 2025 @05:54PM (#45087451)
Newer stuff yes and it is great. However from basic rendering to browsers and the most complex applications (CAD, office software, finance, complex solvers like airline planning) are still in C++. Nobody will accept rewriting 35+ years of history.

C++ code bases are really a lot longer-lived than any other software builds upon them. Hence we cannot drop it.

Starting with C++17, I think committee has been doing the language a disservice and piled even more and more unintended complexity by rushing "improvements" like modules.

I don't write C++ anymore due to my team switching to Rust and C only (for really old stuff and ABI). I am really scared of having to return though. Not because I am spoiled by Rust (I am though), but because catching up with all the silly things they added on top and how they interact with earlier stuff like iterators. C++ is a perfect language for unnecessary pitfalls. Newer standards just exploded this complexity.

Re:We need to seriously think about what to do with C++ modules by jonathrg on Sunday August 31, 2025 @06:35PM (#45087712)
I understand and empathize with your fear. C++23 added a type with 22 constructors (http://en.cppreference.com/w/cpp/utility/expected/expected.h...), that ought to be enough to strike fear into anyone.
Re:We need to seriously think about what to do with C++ modules by okanat on Sunday August 31, 2025 @06:43PM (#45087752)
It already has a defect report too: https://cplusplus.github.io/LWG/issue3886 .
Re:We need to seriously think about what to do with C++ modules by samiv on Monday September 01, 2025 @12:15PM (#45093973)
Don't worry, C++25 will deprecate half of them and C++27 will remove them while adding 20 new ones just to be deprecated in C++30.
Re:We need to seriously think about what to do with C++ modules by okanat on Monday September 01, 2025 @12:28PM (#45094112)
C++ standard and deprecation is a contradiction. They almost never deprecate things. Things always go more complex.
Re:We need to seriously think about what to do with C++ modules by samiv on Monday September 01, 2025 @12:32PM (#45094161)
They're deprecating stuff a lot these days. Add in one version and remove in the next.

https://en.cppreference.com/w/cpp/locale/codecvt_utf8.html

https://en.cppreference.com/w/cpp/algorithm/random_shuffle.h...

Re:We need to seriously think about what to do with C++ modules by ra1231963 on Monday September 01, 2025 @12:04AM (#45089331)
I believe with a little (okay a lot of) motivation and a solid LLM, these can all be rewritten faster than everyone says. Especially if it’s gradual.

Mozilla and Dropbox did it. LLMs are good at translating between languages, and writing unit tests to make sure things still work the same.

Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @04:40AM (#45090785)
How much Rust is actually in Firefox these days? The last numbers I see from 3 years ago is 10%.
Re:We need to seriously think about what to do with C++ modules by j16sdiz on Monday September 01, 2025 @05:12AM (#45090968)
> https://4e6.github.io/firefox-lang-stats/

12%. Assume the progress is linear (not logarithmic like most cases), we just need 60 more years to migrate those c/c++ code.

Re:We need to seriously think about what to do with C++ modules by ra1231963 on Monday September 01, 2025 @09:43AM (#45092643)
That was my point — with LLMs the progress would not be at the same slope as with people only.
Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @10:46AM (#45093047)
Have there been any successful attempts yet of translating 'idiomatic' C++ to 'idiomatic' Rust for a large codebase that has been developed over 30 years? What does the output look like? Does the code look maintainable (because mechanical solutions to translate from other languages into Rust exist, the result is just not what a human would write or ever want to work on). Are the prompts to guide the LLM shorter than the actual code base to be translated? Etc etc... eg the idea to use LLMs for migration is quite 'obvious', but the devil must be in the details, otherwise everybody would be doing it by now.
Re:We need to seriously think about what to do with C++ modules by okanat on Monday September 01, 2025 @12:35PM (#45094195)
Idiomatic C++ allows too much "freestyle" and duck-typed code. Rust language basically doesn't support half of the things that C++ allows because there is no type or memory safe ways to achieve them. When translating things into Rust, borrow checker forces you to invert the logic or completely redo the architecture. Oftentimes it requires trying multiple things and evaluating the performance cost, generated machine code quality and interface flexibility. It is quite difficult without an actual person to translate things.
Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @02:00PM (#45095029)
Yeah that's why I'm very sceptical about any 'LLM will magically fix all the things' claims. The mechanical translations basically work by compiling the source language down to LLVM bitcode and then translating that back to another language, it works but is basically assembly code written in a highlevel language, it loses all 'semantics' and is entirely unmaintainable.
Re:We need to seriously think about what to do with C++ modules by ra1231963 on Wednesday September 03, 2025 @01:51AM (#45112604)
Here are a few

https://www.phoronix.com/news/Google-Linux-Binder-In-Rust

https://arxiv.org/abs/2503.23791v1

https://www.darpa.mil/research/programs/translating-all-c-to...

https://link.springer.com/content/pdf/10.1007/s10664-024-105...

> Everybody would be doing it by now

Models and agents have progressed significantly in the last few months. Migrating projects to rust can definitely be a thing in the coming years if there is sufficient motivation. But oftentimes c/c++ devs have aversions to the rust language itself, so the biggest challenge can be an issue of motivation in general.

Re:We need to seriously think about what to do with C++ modules by c0balt on Sunday August 31, 2025 @05:49PM (#45087419)
I'm pretty sure a lot of fields would disagree with you. Last I checked game programming, OS development, embedded development and more were deeply invested in either C or more often C++, especially when RT tasks were involved or vendor-provided compilers are required.

Rust just doesn't have close to the same type of adoption/support yet, especially when considering various embedded platforms.

Re:We need to seriously think about what to do with C++ modules by holowoodman on Sunday August 31, 2025 @06:04PM (#45087524)
There is a big big difference between C and C++. The article is about C++, which is being replaced by Rust imho. C is different, and far more frequently used for embedded or OS development. Don't know about games, but last I looked, stuff like unity was C#, so something else yet again.
Re:We need to seriously think about what to do with C++ modules by grg0 on Sunday August 31, 2025 @06:27PM (#45087661)
Game engines (the thing that C# runs on top of) are written in C++. As is the C#/.NET runtime. As is anything that requires careful management of memory for performance. Application code is perfectly reasonably written in managed languages, but not so for the things that run underneath.
Re:We need to seriously think about what to do with C++ modules by ShinTakuya on Monday September 01, 2025 @07:44AM (#45091915)
There's nothing inherent about C++ that makes it more suited than Rust for game engines though, Rust supports careful management of memory too. Of course, nothing besides inertia (i.e. Libs, existing code, etc.). And that of course is more than big enough of a reason to stick with it.
Re:We need to seriously think about what to do with C++ modules by grg0 on Monday September 01, 2025 @01:38PM (#45094835)
Rust supports careful management of memory at the expense of unsafety, at which point that particular area of the code offers no benefit over C/C++, let alone value in a rewrite. There is no type system for reinterpreting bits of memory.

And for the safe parts, the posts that I've read from people who have spent a non-trivial amount of effort with the language do not paint a clear picture either on whether there was really a benefit to the language overall.

So to say that "the world has moved on" in light of all of this is pure hubris.

Re:We need to seriously think about what to do with C++ modules by jonathrg on Sunday August 31, 2025 @06:20PM (#45087627)
C++ is widely used in embedded. Most compilers support it. Usually you turn some things off (e.g. -fno-rtti, -fno-exceptions) and try to stick to some sane subset of the ++.
Re:We need to seriously think about what to do with C++ modules by com2kid on Monday September 01, 2025 @02:26AM (#45090018)
C++ in embedded is basically C with classes. It is a nice simple language and often doesn't even include templates (or if it does they are very simple data structures that are not even heap allocated).

C++ as C with classes is a pretty good language!

Re:We need to seriously think about what to do with C++ modules by drwu on Sunday August 31, 2025 @06:30PM (#45087680)
It is a pitty that

- GCC switched from C to C++

- CUDA switched from C to C++

But I can understand the decision, and at that time , C++ frontend features and libs were a little bit less horrible.

Re:We need to seriously think about what to do with C++ modules by deeznuttynutz on Sunday August 31, 2025 @09:36PM (#45088647)
It's a pity?

Please explain in detail how alternatives would have worked better for GCC and CUDA. Also, if you could share some historical context about how those alternatives could realistically have been implemented at the time, that would be helpful too.

I love to hear all the "would've" and "should've" scenarios.

Re:We need to seriously think about what to do with C++ modules by j16sdiz on Monday September 01, 2025 @05:19AM (#45090994)
Last time I checked, MSVC don't want to implement C99/newer and instead focus on C++.
Re:We need to seriously think about what to do with C++ modules by drwu on Monday September 01, 2025 @07:10AM (#45091701)
To be fair, MSVC has the most C99 stuffs. What is mainly missing for porting (my) programs is the native complex number. But we have Intel compiler for free on Windows, which is fully compatible with the C/C++ standard and produces faster binaries.

The C++ frontend of MSVC handles (the common) compiler-specific language extensions differently than the other compilers. Besides, its pre-processor behaves differently too. It is now good that there is a clang frontend for MSVC.

Re:We need to seriously think about what to do with C++ modules by feelamee on Sunday August 31, 2025 @10:01PM (#45088766)
haha, fun fact - Unity is C++ code base. You need to look deeper
Re:We need to seriously think about what to do with C++ modules by diath on Sunday August 31, 2025 @06:30PM (#45087677)
There are a number of areas in programming where I'd always choose C++ over Rust - gameplay programming, retained-mode GUI programming and interpreted programming languages to name a few have very complex circular memory models that are somewhat solvable with weak_ptrs or refs stored in member variables passed through constructors but would be absolutely obnoxious to deal with and get right with the borrow checker.
Re:We need to seriously think about what to do with C++ modules by ozars on Sunday August 31, 2025 @10:38PM (#45088930)
Rust has Arc and Weak that can be used for solving it in a similar way C++ does. The primary difference is forcing usage of Mutex to avoid a large class of data race issues IMHO, and that's what makes it harder to get right. Async in Rust is also more mature than C++ coroutines. So there's also that.

https://play.rust-lang.org/?version=nightly&mode=debug&editi...

Re:We need to seriously think about what to do with C++ modules by MindSpunk on Sunday August 31, 2025 @11:35PM (#45089213)
Arc/Weak can only get you so far. The problem I have with Rust in the mentioned domains (gameplay programming, retained GUI, interpreters) is what makes it so good, sharing xor mutability. If you're writing lots of multi-threaded code it's fantastic. If your problem domain is like the ones mentioned it hurts you.

These are all highly non-parallel problems. They don't gain much from being parallel, and because Rust imposes 'restrict' semantics on even single threaded code you end up making it much harder to write code in these domains.

This has been my experience with Rust. Shared mutability is safe on a single thread without 'restrict' on all your pointers, and Rust has limited options to opt into shared mutability (with lots of ergonomic caveats).

Don't get me wrong though, I still think Rust is a great tool. It just has tradeoffs.

Re:We need to seriously think about what to do with C++ modules by zozbot234 on Monday September 01, 2025 @12:50AM (#45089559)
> Rust has limited options to opt into shared mutability

The idiomatic Rust equivalent of a C non-restrict pointer is arguably &Cell<T>. The biggest problem with it is library code that takes &mut T when the likes of &Cell<T> might suffice (because potential aliasing does not affect the semantics of what that Rust code is doing), but this is an acknowledged problem and the Rust project will take pull req's that fix it where it occurs.

Re:We need to seriously think about what to do with C++ modules by ozars on Monday September 01, 2025 @04:03AM (#45090603)
Shared mutability can cause race conditions in single-threaded environments as well, if you need asynchronous contexts where mutations can be interleaved between suspension points. Think of events, message passing channels, I/O, timers, ticks...

If you're sure you're never going to need multi-threaded environment, you have an option as well: Replace std::sync with std::rc, Mutex with RefCell in the above toy example and that's about it.

If you want to use some asynchronous runtime, replace std::sync with tokio::sync, slap async/awaits along with a single-threaded runtime and that's about it.

Of course, the code above is just a toy example and business logic is much more complex in real world, but compare this to what it would take to write same C++ logic in async.

I found Rust's approach massively more ergonomic compared to C++ approach of passing closures around for asio-like IO contexts or coroutine compiler-magic which opens new novel avenues to shoot myself on the foot, well, to the extent I could grasp it.

It's true Rust forces you to pay all this cost ahead of time. It's also true most applications don't require this level of safety really, so it becomes ridiculous to pay it upfront. And even for some that require such high level of safety, you can skip bunch of bolts on a plane door and it will still be a billion dollar company at the end of the day, so...

Re:We need to seriously think about what to do with C++ modules by nurettin on Monday September 01, 2025 @12:29AM (#45089472)
Yes, you could just use Arc everywhere. But then you've spent a large portion of your coding time fighting the compiler and wasting lines for something that will essentially always be single threaded. (a UI event loop)

The C++ solution would be to start the threads and use an MPSC queue (which, ironically, Rust also has) in order to update the UI.

Rust will eventually stumble upon ergonomics and allow portions of code to be specified as single threaded or embarrassingly parallel, but unfortunately the community evolved horse blinders early on and isn't letting them go any time soon.

Re:We need to seriously think about what to do with C++ modules by lenkite on Monday September 01, 2025 @03:05AM (#45090241)
Rust doesn't have computed goto (gcc/clang extension heavily used in interpreters like CPython and LuaJIT for direct threading).

You’re forced into function-call or jump-table dispatch, which tends to be slower.

Re:We need to seriously think about what to do with C++ modules by ozars on Monday September 01, 2025 @04:08AM (#45090627)
huh. I had no idea. Fair.
Re:We need to seriously think about what to do with C++ modules by aw1621107 on Monday September 01, 2025 @04:57AM (#45090883)
> Rust doesn't have computed goto (gcc/clang extension heavily used in interpreters like CPython and LuaJIT for direct threading).

On the other hand, there's the recently-added-to-nightly `become` for guaranteed tail calls, which might work better than computed gotos if CPython is a good example [0]

> When using both attributes [[[clang::musttail]] and preserve_none], Jin's new tail-call-based interpreter inherits the nice performance benefits of the computed-goto-based version, while also making it easier for the compiler to figure out optimal register allocations and other local optimizations.

To be fair I don't think Rust has a preserve_none equivalent yet, but given naked functions are a thing in Rust I'd hope it isn't too bad to support?

[0]: https://lwn.net/Articles/1010905/

Re:We need to seriously think about what to do with C++ modules by account42 on Monday September 01, 2025 @10:34AM (#45092960)
There isn't much reason to use Rust if you just bypass the borrow checker.
Re:We need to seriously think about what to do with C++ modules by spacechild1 on Sunday August 31, 2025 @07:38PM (#45088088)
How did you get that impression? There are many areas where C++ is still the first choice. Also, "legacy maintenance stuff" is quite an understatement, given how much of our world is powered by C++: browser engines, language runtimes, game engines, video editors, DAWs, automation software, medical equipment, flight control software, etc.

If you think that "the world seems to have moved on to Rust", I would recommend to look at the job listings. For example, devjobs.de: Rust: 61, C++: 1546. That's 1:25. Maybe in other countries there are more Rust jobs?

Re:We need to seriously think about what to do with C++ modules by whatevaa on Sunday August 31, 2025 @05:58PM (#45087476)
Borrow checker is actually not very good solution for some domains. I remember reading that it would not be good for writing javascript VM like v8. Something about managing memory which lifetime actually depends on others, not rust code.
Re:We need to seriously think about what to do with C++ modules by fooker on Sunday August 31, 2025 @06:07PM (#45087547)
C++ has a habit of incorporating all the best ideas from it's 'succcessor' languages once these ideas are mature.
Re:We need to seriously think about what to do with C++ modules by Philpax on Sunday August 31, 2025 @06:14PM (#45087604)
A lot of the problems with C++ are more foundational; you can't adopt the changes that newer languages have made, because that would be a new language - and we know this, because that new language's name is Carbon.

There are things you can add, but the rot still permeates the foundations, and much of the newness goes partially unused because they're just not at home in C++. Use of `std::optional` and `std::variant` is, as far as I know, still limited, even in newer C++ code, because the ergonomics just aren't there.

Re:We need to seriously think about what to do with C++ modules by fooker on Sunday August 31, 2025 @06:19PM (#45087620)
optional is heavily used in new codebases.

variant isn't, yet. We'll eventually get some kind of structural pattern matching that will make variant or it's successor more idiomatic.

C++ does have quite a bit of rot, you're right. But that's the price of building technology people actually use.

Carbon doesn't seem to have any fundamentally new ideas, we'll see how well it fares in the wild.

Re:We need to seriously think about what to do with C++ modules by ghosty141 on Monday September 01, 2025 @02:38AM (#45090085)
> variant isn't, yet. We'll eventually get some kind of structural pattern matching that will make variant or it's successor more idiomatic.

Thats the fantastic thing about c++, you can already write an easy to use match, but they just chose not to include that in the stdlib but rather want you write that yourself.

Example:

    match(foo,
        [](Foo&) {
        },
        [&](Bar& bar) {
        },
        [&](const auto& mode) {
            // catch all
        });
Also optional and expected are ergonomic nightmares since there is no "try!" macro like rust has. In general it lacks the infrastructure that is needed to make these types nice to use. It also clashes with other concepts like RAII where you kinda have to use exceptions when it comes to ctors that may fail.
Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @03:27AM (#45090346)
It would be pretty ugly to make this more introspective in C++.

For example, if you had to match patterns to distinguish between these three possibilities when looking at a tree: (val), (val (tree left) (tree right)), (val (tree subtree)). Here, the possibilities are not really separate types, but rather different ways of constructing a type. This sort of pattern shows up in general purpose programming (even meat and potato imperative programming) pretty often.

There was a proposal for this championed by Stroustrup over ten years ago, but it went nowhere IIRC. https://www.stroustrup.com/pattern-matching-November-2014.pd...

Re:We need to seriously think about what to do with C++ modules by tialaramex on Monday September 01, 2025 @08:42AM (#45092286)
The try! macro was deprecated in 2019 so, almost six years ago. And even before formal deprecation the word try is now a keyword and so you'd need to use r#try! to say you want the obsolete macro not the keyword.
Re:We need to seriously think about what to do with C++ modules by account42 on Monday September 01, 2025 @10:45AM (#45093036)
Ergonomics of std::optional and std::variant have nothing to do with bad foundations and all to do with the C++ committee insisting on these being library features rather than language features. I'm sure we will get the syntactic sugar eventually like we did get range-based for loops after having had to deal with macro approximations of foreach forever.
Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @12:12PM (#45093944)
You’re right.

I have some hope that the upcoming compile time reflection will make it easier to implement said syntactic sugar.

Re:We need to seriously think about what to do with C++ modules by ReflectedImage on Monday September 01, 2025 @01:28AM (#45089728)
Rust is basically the wanted fixes to C++ that C++ itself could never adopt for legacy reasons.

So I'm afraid no by definition C++ can't adopt Rust's ideas because Rust's ideas were originally impossible C++ ideas.

Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @03:42AM (#45090455)
> C++ itself could never adopt for legacy reasons.

I agree with your point except for the 'never' qualifier. It was certainly true when Rust was born.

C++ has proven the 'never' part wrong multiple times. I think, by 2030, the only thing that C++ would lack that Rust has right now is the unified toolchain/packaging ecosystem because people are not going to settle that debate.

Everything else is well on its way to be implemented, in one of three forms - core language features (eg: concepts), language features that primarily enable writing more powerful libraries so that you do not have to come up with language features for everything (eg: reflection), and finally tooling support from the compiler as a test bed of what the language could guarantee (lifetime checks and annotations in clang).

Of course Rust is innovating pretty well too, I am very interested in seeing what async/coroutines are going to look like in a few years.

Re:We need to seriously think about what to do with C++ modules by ninkendo on Monday September 01, 2025 @07:31AM (#45091831)
C++ can never change the way move vs copy semantics work, which is precisely the opposite of Rust’s. It’s the basis for rust’s ownership model, and it’s no small difference. Without this I don’t see how C++ can ever get a workable ownership model like Rust’s.
Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @12:08PM (#45093908)
C++ just gives you the option to have it both ways.

You are free to design your library in a way that your users only see one of these.

Re:We need to seriously think about what to do with C++ modules by tialaramex on Monday September 01, 2025 @12:54PM (#45094378)
What developers actually wanted, even years back when C++ didn't have anything named "move" was the destructive move semantic. It's an elegant, easy to understand feature for a language. I like it very much in Rust which has it.

C++ didn't get that. The proposal paper at the time says it's impossible (for C++). But what they did propose was the feature you've seen in C++ today, which they call "move", but it has slightly odd (though convenient to implement, Worse Is Better after all) behaviour.

Now you can make this C++ "move" behaviour out of destructive move, that behaviour is roughly what Rust calls std::mem::take and it's sometimes useful, which is why that function is provided. But, often it's not really what you wanted, and if you actually wanted destructive move but only have this C++ imposter then you need to perform the entire take, then throw away the newly created object. You will find lots of C++ code doing exactly that.

So, no, you can't "design your library" to deliver the desirable property in C++. It's just another of the dozens of nagging pains because of design mistakes C++ won't fix.

Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @01:24PM (#45094690)
I am still confused about what you are claiming to be not possible in C++ when you are designing your library.

Yes, I understand that there’s a lot of bad code out there and C++ happily enables that. But that was not my point.

Re:We need to seriously think about what to do with C++ modules by tialaramex on Monday September 01, 2025 @02:48PM (#45095478)
Maybe it will be clearer if you read and understand my favourite function from Rust's standard library (its full name is core::mem::drop)

    pub fn drop<T>(_x: T) {}
Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @03:51PM (#45096017)
Great.

template <typename T> void drop(std::unique_ptr<T> &&) {}

Also, you don’t really need this because of RAII.

Re:We need to seriously think about what to do with C++ modules by aw1621107 on Monday September 01, 2025 @04:27PM (#45096333)
> template <typename T> void drop(std::unique_ptr<T> &&) {}

So what happens for objects that aren't behind a unique_ptr?

> Also, you don’t really need this because of RAII.

drop() indeed isn't used much because of RAII, but it is handy for those instances where you do actually want to dispose of something "early".

Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @04:35PM (#45096399)
See the other versions I added.

> dispose of something "early".

This is a bit of an anti pattern in C++, but doable of course.

Re:We need to seriously think about what to do with C++ modules by aw1621107 on Monday September 01, 2025 @05:54PM (#45096955)
> See the other versions I added.

I don't think those work either. Not only do neither of those actually end the lifetime of what's passed in, but they have other flaws as well.

> template <std::movable T> void drop(T &&) {}

This literally does nothing. Reference parameters don't affect what's passed in on their own - you need at least something on the other end (e.g., a move constructor) to do anything.

For example, consider how this would be instantiated for std::vector<int>:

    template <> void drop(std::vector<int>&&) {}
> void drop(std::movable auto){}

I believe this is equivalent to passing by value, so this will actually invoke a copy if what's passed in isn't eligible for a move (or if the move constructor is equivalent to the copy constructor, since copyable subsumes movable). Again, consider how this would be instantiated for std::vector<int>:

    template <> void drop(std::vector<int>) {}
> > dispose of something "early".

> This is a bit of an anti pattern in C++, but doable of course.

I don't think you can do quite the same thing in C++ without introducing new scopes.

Re:We need to seriously think about what to do with C++ modules by fooker on Tuesday September 02, 2025 @09:10AM (#45102660)
> Not only do neither of those actually end the lifetime of what's passed in

>For example, consider how this would be instantiated for std::vector<int>:

Great, here you go.

https://godbolt.org/z/9v66n6Ta4

And yes, the compiler will not prevent you from using this value. (clang will eventually, I think)

But clang static analyzer will happily detect it.

https://stackoverflow.com/questions/72532377/g-detect-use-af...

Re:We need to seriously think about what to do with C++ modules by ninkendo on Tuesday September 02, 2025 @09:46AM (#45103062)
I'm not sure why you keep posting snippets demonstrating C++ rvalue references. We all know what those are, and it's not what we're talking about.

We're talking about how the rust compiler uses move semantics to prevent you from using the moved-from value, such that code like this will not compile:

    let f = Foo::new();
    drop(f);
    f.foo(); // Error: use of moved value
C++'s move semantics do not prevent you from using f after you've moved it. On the contrary, it's intentionally allowed. It's not undefined behavior either, it's "unspecified" behavior, which means "behavior, for a well-formed program construct and correct data, that depends on the implementation". This simply means that it's up to the individual type to decide what happens when a value is moved from. (A string becomes an empty string, for instance, or a vector becomes and empty vector.)

Rust's move semantics mean:

- You don't write a move constructor

- Moves are just memcpy

- The compiler enforces the old value can't be used any more

C++'s move semantics mean:

- You must write a move constructor (rvalue reference constructor)

- Moves are arbitrary code

- The language explicitly allows the moved-from value to continue to be used

That there are certain linter-style tools like clang-tidy which can be configured to warn on using moved-from values is irrelevant: The standard explicitly allows it. It's personal preference whether you should make a habit of using moved-from values in your codebase, which is why this will only ever be a linter thing. The C++ standard would have to completely change its mind and retcon moves to mean something different, if they ever wanted to change this.

Now, the beginning of this thread was someone saying "Rust is basically the wanted fixes to C++ that C++ itself could never adopt for legacy reasons". Then you came back with "I agree with your point except for the 'never' qualifier", implying C++ will eventually support Rust's ideas. But move semantics in C++ are precisely the opposite of those in Rust, because rust-style semantics were deemed impossible to implement in C++, even though it's what people actually wanted at the time. So I think it's fair to say C++ will "never" get Rust-style move semantics.

Re:We need to seriously think about what to do with C++ modules by aw1621107 on Wednesday September 03, 2025 @11:10AM (#45116730)
> Great, here you go.

As I pointed out, your drop_new is broken for copyable types. For example, consider std::array:

    auto w = std::array<int, 3>{0, 1, 2};
    drop_new(std::move(w));
    std::cerr << w[0] << ", " << w[1] << ", " << w[2] << '\n';
This prints "0, 1, 2". Rust's drop() doesn't suffer from this flaw.

> And yes, the compiler will not prevent you from using this value.

Yes, that is the point!

This bit:

    std::vector<int> vec {1, 2, 3};
    drop_new(std::move(vec));
    std::cerr << vec.size() << " <- ?\n";
Simply does not compile in Rust [0]:

    let vec = vec![1, 2, 3];
    drop(vec);
    println!("{0} <- ?", vec.len()); // error[E0382]: borrow of moved value: `vec`
[0]: https://rust.godbolt.org/z/GjcMYnEzq

> But clang static analyzer will happily detect it.

One problem is that you're not guaranteed to catch it, similarly to why static analyzers aren't guaranteed to catch use-after-frees.

Re:We need to seriously think about what to do with C++ modules by ninkendo on Monday September 01, 2025 @05:40PM (#45096877)
It’s not just for disposing things, it’s also used to decompose things into their constituent parts… like if I had something representing an HTTP response and it contains headers and a body, I could write a `fn into_parts(self) -> (Headers, Body)` that returns the parts you care about while destroying the response object.

This is useful in the grpc library I use, which has a response type with some metadata about the response, and an “inner” value that represents the thing the grpc method returned. If you just want the inner thing and don’t care about the metadata, there’s a `fn into_inner(self)` that destroys the response and leaves you the inner value.

You can write similar methods in C++ but they require leaving the original value in an “empty” state so that you can avoid expensive copying of things while still letting the moved-from value be “safe” to use. But that affects your API design… you now have to make the methods that fetch the “inner” thing be nullable/optional so that you can represent “oh this was moved from so there’s no body any more”. You don’t have to do that in Rust: moves are just a memcpy and the compiler will reject programs that use the moved-from value.

Re:We need to seriously think about what to do with C++ modules by fooker on Tuesday September 02, 2025 @09:27AM (#45102847)
> compiler will reject programs that use the moved-from value.

Yes, this is something the C++ 'language' is not going to specify other than claiming that it is undefined behavior.

Doesn't prevent compilers from doing it though, clang will happily do this for you right now in most cases.

https://discourse.llvm.org/t/rfc-intra-procedural-lifetime-a...

Re:We need to seriously think about what to do with C++ modules by ninkendo on Tuesday September 02, 2025 @09:59AM (#45103216)
But it's not undefined behavior. That's the key. It's "unspecified behavior", meaning that according to the standard, it's allowed. A program that reuses a moved-from value is considered valid and well-formed by the standard. It merely delegates how to make this work to the individual implementation (a string becomes an empty string, a vector becomes an empty vector, etc.)

The RFC you posted has nothing to do with move semantics, it's about references outliving what they point to (ie. use-after-free, etc) and similar to Rust's borrow checker.

But here's the thing: move semantics and the borrow checker have nothing to do with each other! The borrow checker ensures that borrowed data (ie. &Foo, equivalent to C++'s references) is sound, it's not the part that enforces move semantics. That happens earlier in the compilation, the compiler enforces moves well before the borrow checker phase.

Re:We need to seriously think about what to do with C++ modules by tialaramex on Monday September 01, 2025 @05:21PM (#45096728)
Edited to inject: Actually on second thoughts maybe none of these even do what you thought they did, they're even sillier than I'd assumed.

So funny thing, your C++ 26 solution doesn't do what my Rust function does.

Actually even the first one doesn't, but you likely don't really care about a std::unique_ptr, so it doesn't feel like a difference, and the negligence only really bites when it's not "just" that pointer type.

You see that Rust function destroyed the T. It's gone, no more T. But your C++ function makes a new T, then destroys the old one, so there's still a T, it's not gone after all.

Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @09:22PM (#45098156)
> You see that Rust function destroyed the T. It's gone, no more T. But your C++ function makes a new T, then destroys the old one, so there's still a T, it's not gone after all.

Perhaps your idea of C++ semantics is a bit off?

Re:We need to seriously think about what to do with C++ modules by tialaramex on Tuesday September 02, 2025 @02:30AM (#45099749)
After more consideration I think probably your functions don't do anything at all?

Is that the joke here? That despite everything you didn't understand why core::mem::drop has that definition and so reading the empty body you assumed that you can just not do anything and that'll work in C++ ?

Re:We need to seriously think about what to do with C++ modules by fooker on Tuesday September 02, 2025 @09:09AM (#45102641)
Are you trolling? Or do you genuinely not understand why it might work in C++?

https://godbolt.org/z/58TqTTM37

Re:We need to seriously think about what to do with C++ modules by tialaramex on Tuesday September 02, 2025 @10:07AM (#45103316)
No, I'm not trolling, I'll give you the benefit of the doubt, try this C++:

https://godbolt.org/z/zM3oxjrfn

and contrast this Rust:

https://rust.godbolt.org/z/1rMYcqY65

In your unique_ptr<T> example what you'd hidden (from me? Or perhaps from yourself) was that we're not destroying the unique_ptr, we're just destroying the Foo, and since the unique_ptr is null the destructor for that will be silent when the scope ends.

Re:We need to seriously think about what to do with C++ modules by ninkendo on Monday September 01, 2025 @05:07PM (#45096614)
What’s not possible in C++ is the actual prevention of the moved-from value from being used after the move.

Entire API’s are designed around this: my type that tracks a background task can have a shutdown() method that moves self, so that if you call foo.shutdown(), you can’t use foo any more.

This is more than just preventing a value from being used, it also facilitates making a rule that “moves are only just memcpy()”, and it can actually be enforced:

C++ move semantics require you to write arbitrary code that pillages the moved-from value (like setting the heap pointer of a moved-from value to nullptr) to ensure the old value is safe: rust just says “nope, there’s no constructor, moves are just a memcpy. We will keep it safe by simply not letting code use the old value.”

C++ can never have this unless they offered yet another mutually incompatible form of move semantics (lol, what would the sigil be? &*&?)

Re:We need to seriously think about what to do with C++ modules by tialaramex on Monday September 01, 2025 @09:04AM (#45092401)
Concepts is a particularly hilarious example because what you got in C++ 20 is Bjarne's "Concepts Lite". Bjarne worked quite hard to get rid of the ideas and people behind the much more powerful C++ 0x Concepts, which resembles Rust's trait feature, and then it took a decade to land his worse alternative. That actually is a microcosmic survey of the problem with the language.
Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @12:07PM (#45093898)
So what you’re saying is that it takes time, but works out?

I agree.

Re:We need to seriously think about what to do with C++ modules by aw1621107 on Monday September 01, 2025 @04:20PM (#45096281)
> So what you’re saying is that it takes time, but works out?

Probably depends on what you mean by "works out". I don't think GP would agree that delivering a less capable alternative qualifies.

For example, one major feature C++0x concepts was supposed to have but got removed was definition-time checking - i.e., checking that your template only used capabilities promised by the concepts it uses, so if you defined a template with concepts you could be assured that if the definition compiled it'd work with all types that satisfied the concept. That feature did not make it to C++20 concepts and as far as I know there are no plans on the horizon to add that feature.

Re:We need to seriously think about what to do with C++ modules by fooker on Tuesday September 02, 2025 @09:11AM (#45102670)
C++0x was ~5 years ago.

C++26 concepts has more or less everything you mention, and you can try it out with all the major compilers right now.

Re:We need to seriously think about what to do with C++ modules by tialaramex on Tuesday September 02, 2025 @11:04AM (#45104043)
First lets clear up a thing I've seen a few times on HN probably from people who are new enough to simply not have run into this before.

C++ 0x is what people called the proposed new C++ language standard from about 2005 through 2009 or so under the belief that maybe it would ship in 2008 or 2009. Because you're here, now, you know this didn't end up happening and actually the next standard would be C++ 11. For a little while they even jokingly talked about C++ 0A where A is of course hexadecimal for ten, but by the time it was clear it wouldn't even make 2010 that wasn't funny.

So C++ 0x isn't five years ago, it's about 15-20 years ago and in this context it's about the draft revision of C++ in which for some time the Concepts feature existed, but Bjarne insisted that this feature (which remember is roughly Rust's traits) was not implementable in reasonable time, and frankly was not as much needed as people had believed.

This argument swayed enough committee members that Concepts was ripped back out of the draft document, and so C++ 11 does not have Concepts of any sort. Because this particular history is from the relatively recent past you can go read the proposal documents, there might even be Youtube videos about it.

OK, so, now you at least know what these terms mean when other people use them, that can't hurt.

As to your next claim er, no, not even close. Barry Revzin wrote a really nice paper connecting the dots on this, which probably passed into legend specifically for saying hey C++ 0x Concepts are the same thing as Rust traits. C++ proposal paper P2279 is what you're looking for if that interests you. That'll be less confusing for you now because you know what "C++ 0x" even means.

Now, Barry wrote that paper in the C++ 23 cycle, and we're now at / just past the end of the C++ 26 cycle, but I assure you that nothing relevant has changed. You can't magically have model checking in C++ that's not there. You can't provide concept maps, it's not in the language and so on.

Re:We need to seriously think about what to do with C++ modules by aw1621107 on Wednesday September 03, 2025 @10:52AM (#45116511)
> C++0x was ~5 years ago.

You're quite a bit off. Tialaramex covered this well enough.

> C++26 concepts has more or less everything you mention, and you can try it out with all the major compilers right now.

Uh, no. No, it doesn't. Here's an example I wrote up earlier that demonstrates how concepts (still) don't have definition-time checking:

    #include <concepts>

    template<typename T>
    concept fooable = requires(T t) {
        { t.foo() } -> std::same_as<int>;
    };

    struct only_foo {
        int foo();
    };

    struct foo_and_bar {
        int foo();
        int bar();
    };

    template<fooable T>
    int do_foo_bar(T t) {
        t.bar(); // No definition-time error despite fooable not specifying the presence of bar()
        return t.foo();
    }

    // Succeeds despite fooable only requiring foo()
    template int do_foo_bar<foo_and_bar>(foo_and_bar t);

    // Fails even though only_foo satisfies fooable
    // template int do_foo_bar<only_foo>(only_foo t);
Here's Clang 21.1.0 compiling this in C++26 mode: https://cpp.godbolt.org/z/znPGvcTqs . Note that as-is the snippet compiles fine, but if you uncomment the last line you get an error despite only_foo satisfying fooable.

Contrast this with Rust:

trait Fooable { fn foo(self) -> i32; }

fn do_foo_bar<T: Fooable>(t: T) -> i32 { let _ = t.bar(); // error[E0599]: no method named `bar` found for type parameter `T` in the current scope t.foo() }

Notice how do_foo_bar didn't need to be instantiated for the compiler to catch the error. That's what C++ concepts are unable to do, and as far as I know there is nothing on the horizon to change that.

Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @04:48AM (#45090828)
Rust has been copying plenty of bad features from C++ too though.
Re:We need to seriously think about what to do with C++ modules by fooker on Monday September 01, 2025 @12:09PM (#45093920)
I think this is the trap that turned away many people from D.
Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @04:47AM (#45090821)
...and unfortunately butchering those good features beyond recognition, see C++20 vs C99 designated init. Typical "design by committee" outcome though :/
Re:We need to seriously think about what to do with C++ modules by fooker on Tuesday September 02, 2025 @09:15AM (#45102723)
Agreed. Design by committee has been the thorn for a long time, hence efforts like Carbon, Circle, and cpp2.

But as it stands, I expect that whatever innovations these languages produce will be picked up by C++ in ~5 years.

Re:We need to seriously think about what to do with C++ modules by InCom-0 on Sunday August 31, 2025 @06:31PM (#45087688)
Have a look at any serious job postings. C++ jobs outnumber Rust jobs somewhere around 50:1.

Internet hype meets actual industry reality :-).

Re:We need to seriously think about what to do with C++ modules by tandr on Sunday August 31, 2025 @07:50PM (#45088146)
Or could it be (or to be a bit of devil's advocate) that Rust projects require only 2% (1/50) of manpower, comparing the same project using C++?
Re:We need to seriously think about what to do with C++ modules by lyu07282 on Sunday August 31, 2025 @08:57PM (#45088451)
I couldn't even tell if you are joking. Rust is weird, its like a programming language people developed a parasocial relationship with. If a Twitch streamer was a programming language it would be Rust. Perhaps its just people being immature I don't know.
Re:We need to seriously think about what to do with C++ modules by feelamee on Sunday August 31, 2025 @09:49PM (#45088706)
> I couldn't even tell if you are joking

I'm thinking the same about your comment :D

Re:We need to seriously think about what to do with C++ modules by lyu07282 on Sunday August 31, 2025 @10:50PM (#45088992)
It's a programming language with a fandom, isn't that odd? ^^
Re:We need to seriously think about what to do with C++ modules by tandr on Monday September 01, 2025 @02:04PM (#45095075)
I was, but I guess adding /s at the end in these cases is a requirement nowadays...
Re:We need to seriously think about what to do with C++ modules by Dylan16807 on Sunday August 31, 2025 @11:39PM (#45089222)
That's not devil's advocate, that's just being silly.
Re:We need to seriously think about what to do with C++ modules by SkiFire13 on Monday September 01, 2025 @02:04AM (#45089886)
Even if the basic idea might make sense there's no way the number if anywhere close to 1/50. Even 1/2 sounds unrealistic.
Re:We need to seriously think about what to do with C++ modules by boppo1 on Sunday August 31, 2025 @05:46PM (#45087391)
Sarcasm?
Re:We need to seriously think about what to do with C++ modules by grg0 on Sunday August 31, 2025 @06:24PM (#45087649)
These comments only reflect the person's narrow view of the "world", along with a generous dosage of pride to state things about which one does not know. If every OS and driver and much of the backbone of society is "legacy maintenance stuff", then the statement is certainly correct.
Re:We need to seriously think about what to do with C++ modules by bukotron on Sunday August 31, 2025 @05:48PM (#45087407)
Rust does not solve any problem existing in expecienced C++ developer career. You dont write modern C++ code in such a way memory leak or dangling pointer is possible at all)

This is exactly WHY we dont see a rush movement of C++ developers to Rust throwing away everything for Rust. Rust is trying to solve problems that already not exist 99.9999% of time in modern C++ code style and standards.

Also, some day C++ compilers or tooling will get its own Borrow Checker to completely forget about Rust - this will be done just for fun just to stop arguing with rust-fans :)

Re:We need to seriously think about what to do with C++ modules by okanat on Sunday August 31, 2025 @06:03PM (#45087523)
This is a very bad take. Rust does solve very real problems in C family languages.

The number of people I met in Rust conferences that rewriting at least parts of rather big C++ codebases weren't small either.

However, there is still big amount of code that is purely C++. Many of the older code bases still use C++03-style code too. Or they were written in the OOP design pattern golden era that requires huge reactors to adapt functional / modern code. Anything with Qt will not benefit from smart pointers. Even with Qt 6.

Rust cannot solve these problems since the challenges are not purely technical but social too.

Re:We need to seriously think about what to do with C++ modules by r_lee on Sunday August 31, 2025 @06:56PM (#45087821)
I would say though that Rust has already had a profound social effect which has probably enabled those rewrites etc. It wasn't too long ago that it was brushed aside as noise yet now its gaining real momentum
Re:We need to seriously think about what to do with C++ modules by Philpax on Sunday August 31, 2025 @06:01PM (#45087502)
Thinking that experience with C++'s many flaws will save you from running into them is delusional - just look at the number of CVEs in projects maintained by world-class C++ programmers.

No amount of fallible human vigilance will stop you from forgetting the existence of a C++ quirk in the code you're rushing out before heading out for the night. Human oversight does not scale.

Re:We need to seriously think about what to do with C++ modules by InCom-0 on Sunday August 31, 2025 @06:45PM (#45087768)
This is true ... except that Rust doesn't actually do any better in that regard.

Rust solves 1 category of problems in a way that is not without its costs and other consequences. That is it. There are projects where this is very important, there are other projects where its virtually useless and the consequences just get in the way. It is not magic. It doesn't make anything actually 'safe'.

Re:We need to seriously think about what to do with C++ modules by feelamee on Sunday August 31, 2025 @09:57PM (#45088749)
sure, but don't forget that rust gives us also a nice tooling, functional syntax sugar like pattern matching, enums, monads; and other more or less useful things like explicit lifetimes
Re:We need to seriously think about what to do with C++ modules by InCom-0 on Monday September 01, 2025 @02:40AM (#45090099)
Of course. This is kind of to be expected as it has the benefit of hindsight of 25+ years. It is infinitely easier to design better things with all the accumulated experience and know-how of what works and what doesn't under your belt. It would have been truly horrifying if that had not been the case.

That being said, Rust is really about lifetimes. That's the big ticket selling point. My point above was that 1) it isn't a silver bullet and 2) it can be a real hindrance is many applications.

Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @04:33AM (#45090757)
Tbh, only a mother can love Rust's syntax. It has even more "punctuation" than C++.
Re:We need to seriously think about what to do with C++ modules by dlahoda on Monday September 01, 2025 @01:15AM (#45089672)
what is this 1 category?
Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @04:52AM (#45090861)
When looking at the number of CVEs you'll also have to take the amount of code written in that language into account, a language which has no users also has no CVEs ;)

I bet there's easily tens of thousands of times more C++ code than Rust code out there.

Re:We need to seriously think about what to do with C++ modules by flohofwoe on Monday September 01, 2025 @04:29AM (#45090737)
Rust isn't exactly famous for its fast build times. It's probably the only language that actually builds slower than C++, which is an impressive achievement on its own ;)

And give Rust one or two more decades and it will be the same messy kitchen sink language as C++. If anything, Rust is speedrunning C++ history (with the notable exception of fixing static memory safety of course).