35 ms·
C++20 has been approved
- melling 6y agoThere are a lot of changes: https://en.wikipedia.org/wiki/C%2B%2B20 https://en.wikipedia.org/wiki/C%2B%2B20 https://en.cppreference.com/w/cpp/compiler_support https://en.cppreference.com/w/cpp/compiler_support
- mehrdadn 6y agoFor those interested: the two biggest features are probably coroutines and modules.
- The_rationalist 6y agoI wonder how their coroutines compare with rust async await, in expressiveness, performance and ergonomics
- steveklabnik 6y agoOne 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.
- Matthias247 6y ago> 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.
- gpderetta 6y agostd::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.
- butterthebuddha 6y agoConcepts and ranges are pretty big too.
- mehrdadn 6y agoYeah 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.
- shash 6y agoDepends on your perspective. Concepts are great if you’re writing libraries and you need to give your user better errors.
- mehrdadn 6y agoYeah 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.
- shash 6y ago
- The_rationalist 6y agoExcept 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
- edflsafoiewq 6y agoWhich features in particular?
- The_rationalist 6y agoThe 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/p0722r3.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p072...
- beagle3 6y ago> 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.
- pjmlp 6y agoIndeed, but there is so much to do on the ecosystem.
- Matthias247 6y agoRight. 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.
- algesten 6y agoWere the module problems ironed out? I remember reading some scathing criticism of the early designs.
- bvaldivielso 6y agoI think that many of the outstanding issues are not related to how modules are specified by the standard, but rather how vendors are going to implement them. Some details that will affect usability and tooling are not baked-in into the standard. In that sense, it's possible that those problems you mention have not been ironed out yet, even if the standard has been approved.
- summerlight 6y agohttps://vector-of-bool.github.io/2019/03/04/modules-doa-2.html https://vector-of-bool.github.io/2019/03/04/modules-doa-2.ht... The issue was that there's no language level definition of module implementation and layout so every build system may need to handle every single module implementation differently. The standard body decided to publish a technical report on this issue to ensure some level of interoperability across module implementations.
- henearkr 6y agoI can't wait (normal) GCC to support modules! (it already does but in a dev branch version, not very handy as it requires a full reconfiguration of the environment variables to switch between this and the normal GCC) The status is here: https://gcc.gnu.org/wiki/cxx-modules https://gcc.gnu.org/wiki/cxx-modules
- hackingthenews 6y agoEven as a C++ programmer with too much spare time, it has become obvious that learning and using all of C++ is beyond impractical. To the point where I find that my C++ code more than ever before looks like C. Nowadays I am using C++ mainly for the high-performance libraries, and only if I can't archive the same in jit-ed python, cython or rust. The Java strategy of being very conservative about what you add to the frontend would have done C++ a lot of good after C++11 (or even before that).
- rightbyte 6y agoThe notion of using C++ like "as C but with namespaces, std::vector, string and map" is way underrated.
- pansa2 6y agoThat’s your choice of C++ subset, but the problem with these huge languages is that everyone chooses a different subset to use. Sticking to plain C has the advantage of probably being the subset of C++ (I know, it’s not technically a subset) that’s in widest use.
- krychu 6y agoI’d add that deciding on the subset is a high cost operation. Design of such a smaller subset-language will likely not be on par with language that is small by design.
- taneq 6y ago> (I know, it’s not technically a subset) Is it not? I thought C++ was explicitly a strict superset of C?
- einpoklum 6y agoSyntactically, it almost was; and even this is no longer true today with C11 and later, More importantly, though - the syntax is not what matters. The languages are very very different in idiomatic use. You just don't write programs the same way with C and with C++. And the gulf between the two only expands with time. A decent C program is almost certainly a poor C++ program, in terms of idioms, utilization of library facilities, and sometimes even in terms of performance (!).
- linkdd 6y agoFinally concepts ! But the module spec feels a bit weird ( https://isocpp.org/files/papers/n4720.pdf https://isocpp.org/files/papers/n4720.pdf ). I mean, "import M" should create a M namespace with the imported symbols. Here, it's more like "from M import *" in Python.
- pjmlp 6y agoC++ already has namespace for that.
- HelloNurse 6y agoPython collapses the name of the module (usually the name of a file) and the name of the "namespace" on the importing side, while C++ modules add a level of indirection by specifying arbitrary namespaces for module content (independent of the name of the module). Evidently, the complexity of finding the same namespace in multiple compilation units has been considered acceptable for C++ modules because it was already handled decently.
- IshKebab 6y agoThis fixes so many long-standing annoyances! Modules at last. String literals as template parameters. Ranges! C99 designated initialisers! Can't wait to be able to use this in about 5 years :-D
- The_rationalist 6y agoYou can already use 90% of this with a compiler flag.I used concepts years ago
- shash 6y agoLooks like most of these features are already supported by the major compilers. A few might be hiding behind a flag or three but I expect them to be defaulted soon.
- IshKebab 6y agoRight it's probably fine for hobby projects but for commercial projects you have to wait until it's supported on all platforms and the majority of your customers are happy with you using it. It can take a long time (e.g. std::optional was only supported on Mac in 2019ish).
- frou_dh 6y agoCool that real modules are now official. Preprocessor textual inclusion should have been left behind in the previous century!
- gmueckl 6y agoThe preprocessor is not gone. It still exists in the context of modules. Figuring out the details of that interaction has been a major issue when defining modules and the result is quite complex. I doubt that we will see clean modules that are free of preprocessor shenanigans, especially when writing any non-trivial portable library. I am no prophet, but I currently assume that unless we get hygienic module friendly macros and better conditional compilation on a syntactic level than dumb textual #ifdef, modules will only end up adding complexity in practice.
- gpderetta 6y agoThe biggest reason that macros are not going away is backward compatibility (a very important reason of course). Adding hygenic macros or "module friendly" macros would not help one bit with that. Also I belive that macros do not escape modules by default. That's a huge improvement.
- in3d 6y agoCompiler support: https://en.cppreference.com/w/cpp/compiler_support https://en.cppreference.com/w/cpp/compiler_support Some features listed as supported are in their experimental versions still but it’s coming along pretty well.
- beagle3 6y agoI've left C++ for C (and Python, and K, and others) somewhere around 2007 when it only started getting crazy. I sort-of keep track of the changes to the standard like lambdas, without which you can't read modern code. But assuming I lose my sanity again and decide to go back to doing C++, which resource would you recommend for properly catching up the last 15 years of C++ changes?
- pjmlp 6y agoBjarne's Tour of C++ 2nd Edition book.
- vanderZwan 6y agoSeconding this. It's a very clean and accessible overview. If the code examples in it were your only exposure to C++ you'd never guess how much of a mess things can turn into "out there" in the wild.
- coldpie 6y agoA psychiatrist.
- uwagar 6y agoi'd second that.
- nikki93 6y agoTour of C++ by Bjarne Stroustrup as others have mentioned. Effective Modern C++ by Scott Meyers (esp. for the chapters on smart pointers). Arthur O' Dwyer's "Mastering the C++17 STL" David Vandervoode and Nicolai Josuttis etc.'s "Templates the Complete Guide" The first chapter of "Elements of Programming" by Stepanov, and maybe the rest if you want. I think also, just kinda glancing over the spec. Bjarne's paper on the evolution of C++ from 2006 to 2020 is also important I think: https://dl.acm.org/doi/10.1145/3386320 https://dl.acm.org/doi/10.1145/3386320 As for talks, https://www.youtube.com/watch?v=XS2JddPq7GQ https://www.youtube.com/watch?v=XS2JddPq7GQ is great for value categories and move semantics https://www.youtube.com/watch?v=St0MNEU5b0o&t=6s https://www.youtube.com/watch?v=St0MNEU5b0o&t=6s also move semantics, and also part 2 of that if you want I'll add more talks here in a bit! And honestly if you've liked C, you can get far by just starting with that then slowly adding things you want. I think the first things one may end up reaching for are std::vector / std::string etc.; then being able to attach methods to types, then some templates, some auto, some lambdas. The old vtables / RTTI approach I basically never use anymore.
- andi999 6y agoIf msvc would support a more modern C, i predict a lot of people would use this instead of C++. (Thanks Herb)
- flohofwoe 6y agoC11 support is on the roadmap since a few weeks ago: https://developercommunity.visualstudio.com/idea/387315/add-c11-support.html https://developercommunity.visualstudio.com/idea/387315/add-... I hope this also means the MSVC team had a change of mind regarding this very unfortunate blog post which IMHO did more damage to C than anything else in the last 20 years: https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an...
- andi999 6y agoYour first link: that would be great, but somehow I can only find a statement that a c11 complient preprocessor is in the roadmap. Can you point me to something specific?
- flohofwoe 6y agoI can't sorry, since I'm not part of the MSVC team. The news that the MSVC preprocessor is C11 compliant made the round long before this specific ticket got the "Roadmap" tag slapped on though (which was on 20-Jul-2020). I guess we need to wait for the first results to show up in MSVC or a statement from MS.
- pjmlp 6y agoAnd rightfully so, C code has no place in modern secure computing besides UNIX kernel clones, and legacy code.
- deleted 6y ago[deleted]
- flohofwoe 6y ago
- dgellow 6y agoHas anyone anything positive to say about C++, and more specifically C++20? We all know the negative parts, any C++ related thread is always full of people repeating them over and over. What about some celebration time for now? A huge milestone for the language has been reached with C++20, can we share something positive about it for once?
- jzwinck 6y agoYes! The spaceship operator (three way comparison a la strcmp) is very welcome, and the no_unique_address attribute will be helpful for people who care a lot about optimization. Simplifications like these really help, and don't take a programming language genius to utilize.
- AlexSW 6y agoAs someone that really likes C++ templates (no other language seems to come close in power for such a compile-time typed system), I'm particularly looking forward to concepts, and I believe (correct me if I'm wrong) that they will make compiler errors for templated code much more straightforward, which will be a nice quality of life improvement even if not using concepts directly.
- greggman3 6y agoThat strikes me as Stockholm Syndrome. No one in their right mind would consider C++ templates a good design for a meta-language. In fact the majority of older template patterns aren't based on good language design but hacks around conditional compiling via arrays of size -1 and crap like that. A good meta programming language would be one that's as easy to program as a general language. lisp, nim, etc...
- einpoklum 6y agoIt's true the templates weren't designed as a meta language. C++ has a lot of baggage. But, at least, you no longer have to iterate over arrays with size-1, (size-1)-1 etc these days. We have variadic template expansion, we have the (ugly, still not decent design, but useful) std::integer_sequence , and better still - we have pretty strong constexpr capabilities.
- anonymousse1234 6y agoC++ Core Guidelines https://github.com/isocpp/CppCoreGuidelines https://github.com/isocpp/CppCoreGuidelines Bjarne Stroustrup: C++ | Lex Fridman Podcast #48 https://www.youtube.com/watch?v=uTxRF5ag27A https://www.youtube.com/watch?v=uTxRF5ag27A
- pkrumins 6y agoThis is another sad day for us, C++ programmers. Even more features have been added to the language that we didn't ask for and that are nearly impossible to learn and use in practice.
- emkemp 6y ago2020 gets worse and worse.
- sayusasugi 6y agoYou don't speak for everyone, pipe down. Furthermore, nobody is forcing you to use these features.
- yanndsal 6y agoWhat features weren't asked for as opposed to what was implemented that was asked for?
- einpoklum 6y agoGreat, now we just have to wait for about 7-8 years or so for all of these features to be widely adopted, and then we're good to go!
- dgellow 6y agoC++20 compiler support is already quite good among the main compilers (GCC, Clang, MSVC): https://en.cppreference.com/w/cpp/compiler_support https://en.cppreference.com/w/cpp/compiler_support
- je42 6y agonope. I am using C++ 20 features in production already.
- superkuh 6y agoEven worse, all the devs jumping on using the C++20 features right now (like the other commentors in this thread) will make software that's near impossible to compile on a linux distro more than 4 years old. And even if someone compiles on a modern machine with a modern build toolchain there's no certainty that the binaries will run on the aforementioned slightly old distro. C++xx new features and improvements are good. But they come very fast and that futureshock is causing the fever that is containerization "solutions".
- dgellow 6y ago
- zelly 6y agoI'm looking forward to reflection and executors in 23. Then it will basically be the most powerful language. With compile-time reflection, you could add whatever language feature you want--almost as powerful as Common Lisp macros.
- brewmarche 6y agoAs someone who's only had C++98 experience in a code base that leaned more towards a Java style, are there some good guides or books on a more modern C++ style?
- dgellow 6y agoA Tour of C++ 2nd Edition, by Bjarne Stroustrup https://www.stroustrup.com/tour2.html https://www.stroustrup.com/tour2.html > The ``tour'' is a quick (about 240 pages) tutorial overview of all of standard C++ (language and standard library) at a moderately high level for people who already know C++ or at least are experienced programmers. It covers C++17 plus a few likely features of C++20. I will add that it's a good way to have an overview of modern C++, you can then go deeper on some topics with more specialized resources. It's a good read IMHO, with a good pace.
- brewmarche 6y agoThank you!
- dgellow 6y agoYou're welcome :) If at some point you're looking for more specific resources, I would suggest to ask on the C++ subreddit. I found people there to be helpful.
- FpUser 6y ago>"Even as a C++ programmer with too much spare time, it has become obvious that learning and using all of C++ is beyond impractical." Who says on must know and use all the features of a language (especially the monster like C++) to write good code / software. Check what you really need for your particular task and be happy. I've found modern C++ a breeze to use as long as I do not pretend to be a language guru. Writing some sophisticated STL like libraries is highly specific and involved task and person/s doing that are not doing much of anything else so they would know all the of relevant C++. Using those libraries however is way way easier, the code looks pretty clear. I use modern C++ to write various business servers. To me it is a single tool or two with clear and relatively easy subset of a powerful language. When I compare it with the monstrosity of some standard "modern" back-end stacks/toolsets C++ is starting to look like a small fish.
- gumby 6y agoI don’t know why this was voted down. C++ has a lot of tools for library writers which are unnecessary when writing application code but make libraries more adaptable. Also c++ keeps a lot of backward compatible code so old code continues to run (so there’s less of the Python 2->3 anguish). English is full of words I don’t use on a daily basis but that doesn’t make it bad. (Not picking on python btw, just using a prominent example of an alternative path)
- FpUser 6y ago>"I don’t know why this was voted down" I suspect it was not because of C++ but me mentioning a monstrosity of some modern back end stacks. I think people get attached to the way of doing things and it becomes akin to religion.
- ausjke 6y agoI'm actually actively learning modern c++ these days, and I like it so far. On another note, Bjarne is 70 year old and he is still very active in c++ new features, very impressive. That gives me a hint that as a software engineer, I can be productive for many years to come as long as I keep studying. I also hope Linus still maintains Linux kernel in the next 20 years.
- rambojazz 6y agoWell, wasn't there any other more informative link than a tweet?
- dgellow 6y agoThat’s a tweet from a committee member, just when the version had been approved. You can find a lot of resources in the other comment threads if you want to see more details.
- gugagore 6y agoI am reminded of this: https://www.devever.net/~hl/nexuses https://www.devever.net/~hl/nexuses "[...] because nobody programs in C++ — everyone programs in their own unique subset of the language. If you are told a program is written in C++, that leaves the following questions: Does it use, or eschew, exceptions? Does it use, or eschew, C++ RTTI? Does it use operator overloading? Does it use, or eschew, the STL? Does it use, or eschew, C++11/C++14/etc. features? Does it use templates? Does it use virtual functions? Does it use multiple or virtual inheritance? Does it use static objects with constructors?"
- b20000 6y agoisn't that great? nobody speaks English. we all speak our own subset of the language.
- lifthrasiir 6y agoAnd yet people in cough some cough countries insist that their English is The English. Likewise, if we had and used a widespread definition of subsets of C++ it'd be pretty much okay. Haven't JavaScript gone this way ("good JavaScript")?
- b20000 6y agohow much software in the world is written in C++, vs written in javascript, and how much of that is actually relevant, in that it contributes something meaningful to society?
- Anka33 6y agoLook at all the weak minded beta programmers complaining about C++ complexity, your weakness disgusts me.