News for hackers. Stuff that matters.
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.