News for hackers. Stuff that matters.
Re:C++20 has been approved by melling on Sunday September 06, 2020 @04:15AM (#24389869)
Re:C++20 has been approved by The_rationalist on Sunday September 06, 2020 @06:12AM (#24390303)
Except for the memory model, rust is loosing a lot of it's competitive advantages over C++ here and more interestingly: C++ is pushing the frontier on what new features/optimizations should a high performance language get. I feel like some of those features will allow new kind of optimizations and it could have an impact on the performance of everyday programs like chromium or Firefox. Of course the adoption will take a few years

I really hope that Rust compiler team will investigate in great depth the expert changes in C++20 and backport the relevant stuff into rust

Re:C++20 has been approved by beagle3 on Sunday September 06, 2020 @06:34AM (#24390379)
> Except for the memory model, rust is loosing a lot of it's competitive advantages over C++ here

Rust's memory model is by far its most significant advantage over C++, and it cannot be added to C++ without breaking all backwards compatibility.

Re:C++20 has been approved by Matthias247 on Sunday September 06, 2020 @12:27PM (#24392454)
Right. Rust is the only of those languages where I feel comfortable handing tasks out to junior programmers and NOT having to hunt for memory errors later in code reviews. That’s such a great reassurance and productivity gain for everyone that it seems hard to go back.

In addition to that it also protects against the majority of multithreading issues - which is something not even available in managed languages like C# or Java.

Re:C++20 has been approved by pjmlp on Sunday September 06, 2020 @06:46AM (#24390440)
Indeed, but there is so much to do on the ecosystem.
Re:C++20 has been approved by the_mitsuhiko on Sunday September 06, 2020 @07:11AM (#24390548)
I don't think Rust is in any way losing anything by whatever is happening in the C++ world. With every iteration of the language it's biggest downside is becoming worse: it's a complex language very few people fully comprehend these days.
Re:C++20 has been approved by jb1991 on Sunday September 06, 2020 @08:36AM (#24390927)
> it's a complex language very few people fully comprehend these days

Well I see your point, the reality is that this statement is also true of rust itself!

Re:C++20 has been approved by tlamponi on Sunday September 06, 2020 @09:05AM (#24391045)
Not really, it even lost quite a bit o baggage when becoming 1.0.

The features it gets now are all mostly on the roadmap since quite a bit and neither surprising nor do the feel like a huge pile of "everything I can imagine" thrown together.

Rust is several order of magnitudes away become such clogged like C++ is, and also its feature acquisition accelerates slower than the one of C++. The only thing slower than that acceleration is probably its compile speed, but here C++ isn't really better. :)

From observing both since a bit I just really got the feeling that rust adoption of things was more thought out to fit the existing language and ecosystem, not 100% perfect, but much more fitting than what C++ did, i.e., mixing in every thing they could get their hands on, while some actually made things better, a lot just do not go well when being together.

Re:C++20 has been approved by moldavi on Sunday September 06, 2020 @02:04PM (#24393294)
As much as I enjoy Rust, I have to agree with GP. Rust team needed to add a lot of complexity to the language to allow us to solve the borrow checker puzzles, such as Pin, Cell/RefCell, split_at_mut, the list goes on, and keeps growing.

In my modern C++ projects, I only really need to know std::move and std::unique_ptr.

The real question to ask is how much complexity one's willing to pay for memory safety. I'm willing to pay it, but with open eyes about the complexity cost I'm paying.

Re:C++20 has been approved by pjmlp on Monday September 07, 2020 @03:24AM (#24397341)
Until Apple, Google, Microsoft, NVidia, Khronos, Sony, Nintendo, ARM have Rust on their SDKs alongside C++, their communities will keep focusing on C++ for high performance code.
Re:C++20 has been approved by the__alchemist on Sunday September 06, 2020 @12:18PM (#24392386)
Another thing not to be underestimated: C and C++'s build tools and dependency process can be intimidating for new users. Rust uses a configuration file to specify dependencies and compile options, then runs with `cargo run`. This is notable on embedded, where the process for Rust is similar as non-embedded, with the addition of installing a CLI app like `probe-run`, and adding a config file specifying memory available. On C and C++, vendor-specific IDEs (Like STM32 cube) are appealing to configure the build process... and the starting files created with these are complicated, even for `hello world`.
Re:C++20 has been approved by vanderZwan on Sunday September 06, 2020 @08:53AM (#24390998)
Maybe it's just me, but if a programming language successfully pushes other languages to catch up or even outdo them in terms of language design, then I would not call that "losing". In the long run we all benefit from that.
Re:C++20 has been approved by OnlyOneCannolo on Sunday September 06, 2020 @07:38AM (#24390686)
Rust's main competitive advantage is memory safety in a compiled language with native performance.

If you're switching to C++ because of performance, you should have probably picked C++ instead from the beginning.

Re:C++20 has been approved by edflsafoiewq on Sunday September 06, 2020 @06:18AM (#24390328)
Which features in particular?
Re:C++20 has been approved by The_rationalist on Sunday September 06, 2020 @06:26AM (#24390357)
The likely / unlikely attributes and the constexpr advances, regarding performance, for example.

But many performance gains will be automatic e.g http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p072...

Re:C++20 has been approved by mehrdadn on Sunday September 06, 2020 @05:28AM (#24390121)
For those interested: the two biggest features are probably coroutines and modules.
Re:C++20 has been approved by butterthebuddha on Sunday September 06, 2020 @05:53AM (#24390229)
Concepts and ranges are pretty big too.
Re:C++20 has been approved by notacoward on Sunday September 06, 2020 @08:42AM (#24390951)
These seem to be the four features everyone's talking about: coroutines, modules, concepts, and ranges. Of these, modules and concepts are mostly being praised not so much for adding anything as for removing pain points - super slow compilation in the case of modules and super awful error messages in the case of concepts. Along with ranges, these merely increase parity with other languages that never had these gaps/warts to begin with. The only one of the four that seems like a feature rather than a bug fix is coroutines.

So yay, these are improvements, and welcome just like improvements to any legacy system always are. Kudos to the "maintenance programmers" of language design.

Re:C++20 has been approved by mehrdadn on Sunday September 06, 2020 @06:11AM (#24390302)
Yeah it's all subjective. On my end concepts kind of seemed unnecessary to me (albeit useful) given you could just write unconstrained templates. In fact I still expect most people to do that a large fraction of the time just due to the diminishing returns. Ranges seemed more necessary but not particularly sophisticated machinery to live without (and I'm curious how much performance impact the templates have too). Whereas coroutines and modules are not only incredibly useful but also quite sophisticated underneath and also quite difficult (or I guess impossible, in the case of modules) to simulate by hand.
Re:C++20 has been approved by gpderetta on Sunday September 06, 2020 @06:58AM (#24390494)
Concepts are great because we can get rid of all tag dispatching/partial ordering/SFINAE hacks with a more principled (and understandable) approach.
Re:C++20 has been approved by jcelerier on Sunday September 06, 2020 @07:03AM (#24390513)
with concepts you also get

    void f(auto x) { } 
or

    void f(Sortable auto x) { } 
as a bonus, which is super neat imho and will reduce the amount of template<> syntax verbosity by a good margin
Re:C++20 has been approved by lenkite on Monday September 07, 2020 @03:24AM (#24397346)
Concepts will stop people in HN complaining they cannot understand templates. It significantly reduces the template magic in C++ libraries.
Re:C++20 has been approved by shash on Sunday September 06, 2020 @06:49AM (#24390453)
Depends on your perspective. Concepts are great if you’re writing libraries and you need to give your user better errors.
Re:C++20 has been approved by mehrdadn on Sunday September 06, 2020 @09:11AM (#24391071)
Yeah I do expect some very well-written libraries (including standard libraries) to pick it up and use it well, which will benefit a lot of people as these will be widely used libraries, but my guess is most other C++ programmers will not use concepts in nearly as many cases as they hypothetically could/should. Maybe they'll write a few concepts to document the occasional clean interface, but that's about it. The reason being that I expect concepts will have underestimated costs: duplicated logic will not only be frustrating but also get out of sync easily, templates will slow down compilation (it's not like we don't have enough of that already), precise constraints end up being difficult to write, and approximations end up being overly strict/loose and result in headaches for users of a library.

They do seem quite beautiful and useful in theory, but I will be very curious to see how much they're adopted.

Re:C++20 has been approved by shash on Sunday September 06, 2020 @10:14AM (#24391441)
> They do seem quite beautiful and useful in theory, but I will be very curious to see how much they're adopted.

For sure! I expect some marquee libraries like ITK to adopt them as soon as it’s prudent (meaning when they can sunset support for older compilers), but most people won’t be using them.

I mean, most people don’t have to even really write templates code...

Re:C++20 has been approved by mehrdadn on Sunday September 06, 2020 @05:11PM (#24394705)
Oh right, but I meant even by people who do write templates haha. I'm wondering in what fraction of cases they'll actually use concepts when they potentially could.
Re:C++20 has been approved by tylerhou on Sunday September 06, 2020 @06:55AM (#24390481)
Concepts are a huge improvement to help new users learn the language. Large, unclear errors are a big put-off.
Re:C++20 has been approved by The_rationalist on Sunday September 06, 2020 @05:52AM (#24390225)
I wonder how their coroutines compare with rust async await, in expressiveness, performance and ergonomics
Re:C++20 has been approved by steveklabnik on Sunday September 06, 2020 @09:46AM (#24391247)
One significant difference is that, in my understanding, C++20 coroutines require an allocation. This can be optimized away but is not guaranteed. Async/await will not allocate in Rust.

Superficially, there's also co_await vs .await.

And, really, async/await is a subset of the C++ feature; you'd want to compare against generators, which aren't yet stable in Rust.

Re:C++20 has been approved by Matthias247 on Sunday September 06, 2020 @01:36PM (#24393055)
> This can be optimized away but is not guaranteed. Async/await will not allocate in Rust.

That makes Rusts solution sound better. But in reality things are a bit more differentiated. Both solutions are not zero cost abstractions for various use-cases. And depending on your use-case one or the other solution might end up being better.

From the user perspective, a big difference is that Rusts `Future`s are lazy (will only do something when polled), while C++ coroutines and the `task` frameworks on top of them are not. Besides influencing the way concurrency can be performed within these environments, this e.g. also has an impact on the stack usage of the solution (Rusts Futures can require a terrible amount of stack just to create the final composed Future).

Then there is the "interface" question. As far as I understand you can have virtual coroutine functions on classes the same way that you can an other functions. But you can't have the same in Rust at the moment. The "async-trait" crate gives you that ability again - but with the drawback that you now have a mandatory allocation (instead of the often elided one in C++).

Overall C++ coroutines are a lot more flexbile than Rusts async/await. The behavior can specified by overriding the promise type, storage type, behavior of co_await, etc. You can make a generator out of it, but also an async function. Rusts async functions are targetted purely at the async function use-case - which might make them a bit easier to understand.

Even within that use-case they are more constrainted. E.g. with C++ coroutines you can do things like "thread-hopping" - `co_await method_that_moves_me_to_other_thread()`. Rusts executor idea wants you to resume your piece of work on the same task (which imho think is nearly always a better idea!).

Another difference is that Rusts "resume/Waker" mechanism is designed for a multithreaded environments - it will take a needless performance hit in a pure singlethreaded shared-nothing environment(think Seastar). With the C++ solution you might be able to avoid that - but at the cost of the framework and language not providing you any thread-safety at all.

Re:C++20 has been approved by gpderetta on Wednesday September 09, 2020 @07:50AM (#24419985)
std::future/promise is hopelessly inefficient, but the new executor framework being proposed for C++23 does provide for lazy evaluation of futures (or its equivalent). In fact lazy evaluation is the default. Executors can interface with coroutines (although the impedence mismatch is real) and the interface is powerful enough that allocation can be avoided in many cases (not via compiler optimization which I find too fragile), but via being able to inplace construct the coroutine frames in other existing regions of memory.

I was very critical of the C++ coroutine design as the extension interface feels very ad hoc and not built from first principles and because of the hard to avoid memory allocation, but people that know better than me seem to think that it is usable in practice with good performance.

I do believe that thread hopping is quite important in case you need access to remote data and want to avoid mutual exclusion.