r/cpp • u/zowersap C++ Dev • Sep 05 '20
C++20 has been approved
https://twitter.com/patriceroy1/status/1302055575140945921?s=2130
u/OnesWithZeroes Sep 05 '20
Well, let's see how cmake and alike will adapt modules.
10
u/zowersap C++ Dev Sep 05 '20
https://gitlab.kitware.com/cmake/cmake/-/issues/18355 track cmake C++ modules support
22
u/matthieum Sep 05 '20
Honestly, that's the feature I am most excited about in C++20.
The lack of macro export is somewhat annoying, so I'll still need a few headers around, but otherwise I am really looking forward to cleaning up the code.
3
u/CoffeeTableEspresso Sep 05 '20
I can't think of a clean way to export macros though. If you're using the preprocessor might as well just embrace it.
2
u/matthieum Sep 06 '20
I don't see any difference between exporting a macro and exporting a template function; really.
6
u/CoffeeTableEspresso Sep 06 '20
The preprocessor happens at an earlier stage and is purely textual. You really complicate modules by allowing exporting macros.
By the time importing is happening you've already parsed things, which can mess up your macros, since they should have already been expanded by this point.
1
u/zowersap C++ Dev Sep 05 '20
AFAIK they already work on the feature, they have already FORTRAN modules, which were the predecessors of C++ modules
5
115
u/Robert_Andrzejuk Sep 05 '20
Now we just need to wait, until all the compilers implement it : https://en.cppreference.com/w/cpp/compiler_support
109
u/windozeFanboi Sep 05 '20
5minutes later....
Is it here yet?
5min
How about now?
Proceeds to keep clacking fingers on the table nervously. Like an 10 pack a day smoker missing a cigarette
32
65
u/azswcowboy Sep 05 '20
Honestly I think it’s impressive that a large chunk of it is already available given the number and scope of updates - and it still being 2020. I expect most will be done by middle of 2021. I think it took the better part of a decade to get the 98 standard really implemented.
6
u/anon_502 delete this; Sep 07 '20
The possible worst case I can imagine could be the implementation being long-tail efforts - each compiler has their different 90% implementation, yet the remaining 10% never gets done until their impending removal.
10
u/Myriachan Sep 05 '20
I wish that chart had bit_cast, since that is the second-most feature of C++20 I’m looking forward to (the first being using enum).
9
u/STL MSVC STL Dev Sep 05 '20
That's available in VS 2019 16.7. It's not yet implemented in libc++, likewise for libstdc++.
4
u/Myriachan Sep 05 '20
Well, the library support isn’t as important as the __builtin_bit_cast, but yeah, thanks =)
6
u/pjmlp Sep 05 '20
That list doesn't list all of them though, there are a couple still missing from embedded space.
4
4
u/Alexander_Selkirk Sep 06 '20 edited Sep 06 '20
One thing that does not seem to be there is build systems which support modules. I searched and from what I've read, ninja and build2 have some support. CMake does not have support: https://stackoverflow.com/questions/57300495/how-to-use-c20-modules-with-cmake
I see modules have the promise that it leads to faster builds if interfaces and implementations are separated, like in, for example, Pascal or Modula 2. I've used a system which compiled EC 61131-3 Strucured Text for PLCs, which is essentially a derivative of Modula, and I can confirm it compiles blazingly fast. However, it seems that C++ has a property which is very different from Modula or, for example, Java, in that to use a class as a data member, its entire definition needs to be known, because the size needs to be known (at least unless one uses the PIMPL idiom which I don't believe is always a desirable solution). And I think this is perhaps one reason for the long compile times in C++ projects, each source file needs to pull in all needed definitions, and this includes all used classes.
There also seem to be serious concerns how modules can be used at all in larger existing projects. I do not have detail understanding, but I believe this is in part because C++20 modules do have some proprties only in common with Fortran's modules. Specifically, unless a source file is build, it is hard or might even be impossible to say which modules it depends on, and where they can be found. The former for example because some part of sources might be OS or hardware dependent, which is so far usually solved by preprocessor macros, conditional includes, and conditional compilation; I am not sure whether modules will allow to get completely rid of them. The latter is because there is no required mapping from imported module names to file names or search paths in the file system. This is different from the existing "#include ..." system because the existing system allows to compile sources in one pass, and allows to start compiling all translation units in parallel, because they don't depend on each other.
Clearly, the dependency issue is not a noticeable problem if one builds a small toy project in which the compiler is called manually, but this could be very different for large projects which use tens of thousands of files.
Here is an article which describes these concerns in more depth:
"C++ Modules Might Be Dead-on-Arrival"
https://vector-of-bool.github.io/2019/01/27/modules-doa.html
I don't know enough about that matter to come to a firm conclusion but superficially, such concerns seem somewhat justified, and according to the article above it might take a very long time to shake all related issues out, perhaps even at the cost of backwards compatibility because it looks like legacy build systems will have substantial difficulty to adapt to the required changes in tooling, and it will not be possible to adapt them all.
Would love opinions from people who know more about that.
5
u/Minimonium Sep 06 '20
About the module finding bit - WG21 strongly claims that they have the fastest possible module scanner implementation, which would avoid such issues as the order of Boost include directories affecting compilation speed on Windows, even with modules requiring to open each file in each directory provided for the module statement with its name instead of file name unlike headers.
1
u/Alexander_Selkirk Sep 07 '20
It would be nice to have some actual data comparing build times with and without modules for building larger systems, such as boost. I don't quite understand how such important changes to the whole ecosystem can be made without rigorously verifying that they bring actual advantages.
2
u/Minimonium Sep 07 '20
Note my comment doesn't imply that I trust WG21 judgment on that one, quite contrary in fact. The mailing list at the time of the DOA blogpost was open, which was a fiasco since core members were rejecting any concerns about the theoretical performance of the suggested design with a wonderful suggestion to implement this design themselves first to prove that it's not sufficient. After that most actual discussions on modules were done outside of said mailing list.
1
u/sequoiajoe Sep 06 '20
...and then wait for it to be usable and standard in my workplace.
Well, see you in a few years, C++20...
9
u/sandfly_bites_you Sep 06 '20
I see a few things that seem nice
using enum -less verbose
[[nodiscard]] for constructors -prevent unnamed scope locks etc
Converting from T* to bool should be considered narrowing: seen this error before..
Default member initializers for bit-fields: they fixed this obnoxious hole in the language
I'm guessing it will be a few years before the big tickets like modules/coroutines are viable and can be assessed..
6
u/obvious_apple Sep 06 '20
What I would like to see is a specified modern subset of the language and a compiler flag to deprecate all the old unsafe stuff. This way you can have all the goods of a modern language and a separate option to compile old code.
13
u/pedersenk Sep 05 '20
I do wish along with these new features there would be a little bit more focus on memory safety.
It seems there hasn't been much more work in this area since TR1 (around C++0x). A suitable observer mechanism to unique_ptr<T> similar to weak_ptr<T> would go a long way!
It is still too easy to dangle iterators, references, even 'this'.
9
u/staletic Sep 05 '20
10
u/pdimov2 Sep 05 '20
This trivial example is caught by all major compilers, although
-Wlifetimeremains the best bet for slightly more complex code. I wrote about this in https://pdimov.github.io/blog/2020/05/17/state-of-c-static-analysis-circa-2020/.2
3
u/_Js_Kc_ Sep 05 '20
I'm disappointed. Wasn't there some talk a while ago about lifetime checking that was supposed to catch this?
8
u/staletic Sep 05 '20
If you're talking about the talk by Herb Sutter, then the linked repo is where the development is happening.
From what I've seen, your example is really surprising. Either the godbolt compiler hasn't been updated in a long time or there's a bug in the lifetime analysis. I guess it's worth opening a bug report in the LLVM fork's repo that I linked.
0
u/pedersenk Sep 05 '20
Nice. I am a big fan of clang's asan but didn't know about some of these newer facilities.
As well as this, the older mudflap and even valgrind are often on the CI to facilitate testing. However we do have a number of projects where our compilers are either older or simply do not have the support for them on the architectures we are using. This is certainly getting better over time however.
For ages, Microsoft's compilers have been the most annoying to use because no valgrid and the basic CRT debug runtime misses so much!
In theory the safety in the standard library would solve that going forward.
2
u/excessdenied Sep 06 '20
A suitable observer mechanism to unique_ptr<T> similar to weak_ptr<T> would go a long way!
I've thought about this as well, but I think it's hard to do in a good way. Apart from how to implement the tracking as others have mentioned, there's also a problem with the access side of it. With weak_ptr you get a shared_ptr out of it when you want to access it (lock), which means you know it's still alive as long as you have that shared_ptr. With an observed_ptr or whatever you want to call it, I don't really see a good way to access it. Something like "if(!observed_ptr.expired()) observed_ptr->doThing()" would be unsafe in threaded and possibly other more complex scenarios.
2
u/pedersenk Sep 06 '20
Yeah this is very true.
I suppose I wouldn't mind getting something functionally similar to a shared_ptr<T> out of it's lock() (perhaps called lock_ptr<T>?), the only difference is that this lock_ptr<T> doesn't increase the reference count (that would make no sense for a unique_ptr), only it actually puts a lock on the unique_ptr<T> so that if it does delete during the lifespan of the locking pointer, an assertion error is given.
Yes, would be useless for preventing a crash, but it would be a useful debugging aid to find incorrect code. However this kind of "error finding" stuff, the C++ standard has not delved into which I feel is a shame.
2
u/excessdenied Sep 06 '20
In my case I'm just using shared_ptr. I know it's not the perfect fit but it works well and there are very seldom any actual issues (although they can be a bit messy when they do appear).
1
u/pedersenk Sep 06 '20
Yep I tend to do exactly that.
However in the past during a code review i have had to explain why use a shared_ptr<T> in place of a unique_ptr<T> when it was not going to be shared. I did mention that the data had a number of observers and they seemed satisfied, it was more just a bit annoying it even got flagged up.
I also note that a lot of literature and projects seem to favour unique_ptr<T> and then use a raw pointer counterpart, which I find illogical and unsafe. It is IMO careless choices like this that contribute to the old opinion that C++ cannot possibly be safe and that we "need" a new language.
2
u/excessdenied Sep 06 '20
However in the past during a code review
I am the one who reviews. :) Nah, but we're just a few people with full control over the code base and we've chosen to go the overusing shared_ptr route. At least in the parts of the code where ownership is complicated.
I also note that a lot of literature and projects seem to favour unique_ptr<T> and then use a raw pointer counterpart, which I find illogical and unsafe.
Yeah. I can understand why and I know reasons why using shared_ptr by default may not be the perfect thing, but at least in our case we gain more than we risk, and things like worse performance doesn't really matter.
4
u/pdimov2 Sep 05 '20
A suitable observer mechanism to unique_ptr<T> similar to weak_ptr<T> would go a long way!
An interesting suggestion, but I don't offhand see a way to implement that. unique_ptr<T> is guaranteed to be the size of a single pointer, so there's nowhere to store the needed metadata. Before the object doesn't work because you can convert unique_ptr<Derived> to unique_ptr<Base2>.
2
u/pedersenk Sep 05 '20 edited Sep 05 '20
Yeah, I do understand there are a few technical reasons for it.
I also know that the standard does not necessarily comment on "debug" runtimes but it might be useful to have something then that only exists at debug time which can deterministically detect access of a dangling pointer to a unique_ptr<T>.
So perhaps in debug time, the smart pointer could contain this meta-data and then in the release build it remains as it is.
In the past I did draft up a prototype here (a debug std runtime): https://github.com/osen/sr1
If you have time, you can see in the tests (yes, some fairly artificial haha!) that for the small functionality it provided, (shared, weak, vector), 70% of errors were undetected without it, but I knew playing catchup with the STL was going to be impossible for me to maintain. At work we use a subset of this in our platform compatibility library that has been "moderately" successful at detecting errors.
If you want to see an example of some of the big hacks deployed to make this possible, check out this: https://github.com/osen/sr1/blob/master/include/sr1/vector#L227 and the operator[] below it XD
3
u/scrumplesplunge Sep 06 '20
it might be useful to have something then that only exists at debug time which can deterministically detect access of a dangling pointer to a unique_ptr<T>.
I forget where I read it, probably some post in cpp a few months ago, but there was some article describing a workflow that a company was using that did something like this. Basically they had their own aliases like owner_ptr<T> and borrow_ptr<T>. In prod they were just wrappers for unique_ptr<T> and T*. In debug, they were wrappers for shared_ptr<T> which had assertions about the reference count in the destructor. The owner_ptr<T> would crash on destruction if it wasn't the sole owner, and could even point at all the places which still held borrow_ptr<T> pointing to the object. I thought that was pretty nifty. Still, it makes all the pointer access slower which could make debug builds of some programs unusable.
edit: phrasing
-1
u/bumblebritches57 Ocassionally Clang Sep 05 '20
Just now?
I thought it was standardized months ago...
8
u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Sep 06 '20
Please have a look at the https://www.iso.org/stage-codes.html. In Prague, we technically voted on proceeding the committee draft (CD) to subject it to a vote to become a draft international standard (DIS), and stopped the technical work on C++20 to just start working on C++23 instead. The incoming ballot results mean two things: it is a DIS now and it will be fast-tracked to become a final draft international standard (FDIS) without another ballot round. After addressing some editorial issues and ISO technicalities, the FDIS will be ready for publication and enter the state of being an ISO standard under publication, hopefully under the name "ISO/IEC 14882:2020".
6
u/breue Sep 05 '20
There are a few stages to the process. What I think you're thinking of is that they voted on the final draft. Usually there are only administrative changes after that, so that's usually when it's more or less final. Now we're really really a standard.
2
u/bumblebritches57 Ocassionally Clang Sep 05 '20
Ahh, that makes sense.
but what is an "administrative change"?
like changing the value of
__cplusplus, or just like changing the copyright date or like what?4
-1
u/helikal Sep 07 '20
I seems the creators of C++ need to refocus on what actually should be the core virtues of this language. Adding ever more features, of course, allows writing code in any style known. But this is hardly a worthy goal as it comes at the cost of making it more difficult to learn, more difficult to read, and more difficult to compose C++ code written in different paradigms. C++ has been used for critical, long-lived stuff, where reliability is crucial. If C++ wants to stay relevant to this kind of applications, a main goal should be greater simplicity because nothing helps more with achieving program correctness.
0
0
Sep 05 '20
[deleted]
8
u/JavaSuck Sep 05 '20
Holy shit are they really doing ranged based for loop?
Range-based for loops have been in C++ since 2011.
3
Sep 05 '20
I don’t know why you’re surprised, there is an addition of an init statement with this standard, but range based for loops are not new.
95
u/zowersap C++ Dev Sep 05 '20
https://twitter.com/sdowney/status/1302108606981173252?s=21