r/cpp • u/Daniela-E Living on C++ trunk, WG21 |đ©đȘ NB • Feb 11 '23
C++23 hits the road!
The sausage is made, the bread is bakedWe now have C++23!With crossing all eyes and dotting all tsThe best C++ ever, for all of ye!
C++23 is what C++20 was meant to be but couldn't that time.
Much of it is already implemented and released in compilers and can be used today, more will become available in the near future.
If you are interested in how C++23 looks like and why you want to use it, you may look at keynotes from the 2022 CppCon and Meeting C++ conferences
Contemporary C++ in Action - Daniela Engert
How C++23 changes the way we write code - Timur Doumler
There is also A Peek at C++23 - Barbara Geller & Ansel Sermersheim to enjoy an learn from.
The specification of C++23 will now proceed to the final ballot and then be officially published, hopefully long before year's end. The committee is busily working on another future release, tentatively C++26, for a couple of months now.
80
u/2MuchRGB Feb 11 '23
Check cppreference if you want to see which compiler supports what.
As a summary: The compiler support is best in GCC, then Clang followed by Apples Clang then MSVC and EDG eccp (I've never heard of this one before). No other compiler seems to have any support so far.
In the Library it is MSVC STL then GCC libstdc++ then Clang libc++ then Apple Clang.
14
u/ShakaUVM i+++ ++i+i[arr] Feb 11 '23
Which one is "import std;" in? That's the main thing I care about, though after looking over the list I see a need for five more versions of const.
22
u/Jannik2099 Feb 11 '23
import stdis in C++23 via P2465R3. Right now, only MSVC STL implements it, but from what I've seen the others should have it soon.13
u/ShakaUVM i+++ ++i+i[arr] Feb 11 '23
Right now, only MSVC STL implements it, but from what I've seen the others should have it soon.
I'm so excited
I just can't hide it
9
5
Feb 11 '23
The thing about Apple clang is weird here. It is clang. It is generally 6mo old at release. So currently, they happen to, it is based on clang-14. Both are on 6mo cycles too. I suspect this March, it will be 15, which is the current clang release.
4
u/gracicot Feb 12 '23
But the weird thing with Apple clang is that the library standard implementation is usually behind the clang version they actually use.
1
Feb 12 '23
when i check the feature macros they are the same right now. i remember in the past they did some weird gating and stuff, like filesystem didnt work on high sierra(maybe the next releases) but the files themselves look mostly untouched.
1
3
u/2MuchRGB Feb 11 '23
It was listed seperatly in the source so I listed it here too.
2
Feb 11 '23
yeah, i check the feature flag macros and far too often cppref is out of date here. the xcode wikipedia entry says which versions match too
20
u/pjmlp Feb 11 '23
clang should maybe focus on being C++20 compliant first, where it is trailing GCC and VC++, before going at C++23 features.
1
u/caroIine Feb 13 '23 edited Feb 13 '23
I'm just glad they were able to ship "typename optional in more contexts" and "Parenthesized initialization of aggregates" before 2023. But it will take years to finish modules...
1
u/knue82 Feb 12 '23
EDG is used by icc afaik
2
u/pjmlp Feb 12 '23
Used to, the new icc is a fork of clang, so most likely not using it anylonger.
2
u/knue82 Feb 12 '23
Ah yes. Heard this as well.
Anyway. EDG used to be a popular (propriety) c++ parser for many tools but nowadays most of them switched to clang.
0
Feb 11 '23
[deleted]
56
u/IAmBJ Feb 11 '23
Microsoft open sourcing their standard library implementation probably has something to do with it
And /u/STL of course
81
u/STL MSVC STL Dev Feb 11 '23
đž Keeping the open-source repo running smoothly is my main job these days!
5
u/ImYoric Feb 12 '23
Out of the curiosity, what are the main challenges?
14
u/STL MSVC STL Dev Feb 13 '23
The most challenging thing is probably reviewing pull requests thoroughly, to avoid bugs in new code and regressions in existing code. We rely very heavily on code review to keep our development velocity high. I've worked on the codebase for 15 years, and I got started essentially as a professional code reviewer, but it still takes a lot of effort.
Other things involved in creating an environment where everyone else can contribute optimally: training people in this code review culture, coordinating with compiler teams to get features and fixes, organizing information like accepted features and LWG issues so it's easy to find things to work on, and handling merging and infrastructure logistics (which tend to be mind-numbing, semi-mechanical work that demands high attention to detail and occasional brief bursts of complexity) since I'm very fast at that and it frees up others to work on things that are way more interesting to them.
19
u/fsb4000 Feb 12 '23
Microsoft STL is Open Source so the cppreference describes the current git status. (as for now it is up to 19.37 Preview 1)
Microsoft compiler is proprietary so the cppreference describes only the current release status (19.34 for now)
I think it also affects
0
Feb 11 '23
[deleted]
9
u/Krnpnk Feb 11 '23
I think their own list hints on usecases: https://www.edg.com/customers
There's lots of embedded compiler and static code analysis companies there. And I thought that the previous versions of the Intel compiler used EDG as well but I may be mistaken.
2
u/wezrule Feb 12 '23
I worked at QA-Systems on that list. They used it for dynamic code analysis for embedded systems to ensure code met various ISO safety standards.
0
Feb 12 '23
[deleted]
9
u/arkadesh_d Feb 12 '23
Cyber Security here. C++20 used all the way and waiting eagerly for C++23 greenlighted by the management.
7
u/donalmacc Game Developer Feb 12 '23
My last company was officially 17 but in reality it was 11 with one or two allowed features. I left them in 2020. My current team is 17 but starting to touch on 20. It's hard when you've got multiple platforms and compiler versions to manage unfortunately
6
u/2MuchRGB Feb 12 '23
I think part of the problem is that a lot of embedded projects use the fork of the chip developer. The last one I used barely had C++14 support.
4
u/CubbiMew cppreference | finance | realtime in the past Feb 12 '23
finance here, using whatever the latest gcc and clang can both do - standards, draft standards, extensions, whatever works.
1
u/caroIine Feb 13 '23
Food Service QA
We are using C++17 extensively and don't see that many reasons to upgrade to C++20 just yet.
33
u/dashnine-9 Feb 12 '23
Can't wait for C++37 when reflection will finally be implemented.
8
u/TrueTom Feb 12 '23
At least we got networking.
1
0
Feb 12 '23
[deleted]
11
u/TrueTom Feb 12 '23
That was just sarcasm. Networking is currently targeted for C++26.
16
u/14ned LLFIO & Outcome author | Committee WG14 Feb 13 '23
I presented a bare minimum viable vehicle for C++ 26 Networking at the Issaquah meeting, made viable mainly by dropping anything to do with async. It got killed. Committee wants async. Committee also wants several ponies and many unicorns in Networking. Committee wants it all, and currently doesn't seem to care if that makes timely delivery impossible. I especially remember in the comments to my paper around the room there were clearly three separate factions, most with the opinion "either it's my networking or I'm strongly against/over my dead body because the networking of the other tribe is the worst and would actively destroy my networking for various vague reasons" (which I mostly suspect were the effects of insufficiently generalised domain expertise to understand/care about other people's networking outside your own domain of expertise). So I bailed out lest I fall on the same sword as Chris K did, and I wish god speed on everybody working on that effort going forward, but it won't include me.
Personally speaking, I would be surprised if Networking will now make 26. The LEWG session basically asked for the kitchen sink plus ponies plus unicorns and of course candy floss, yet it's only Dietmar who would do the work, and I doubt it's feasible for a 26 timeline. I think anything he presents achievable in the limited time will constantly get comments like "but it doesn't do X and without X I'm strongly against". So maybe 29, currently I'm thinking C++ 32 most likely. And quite possibly never, unless something major changes, if I am blunt.
Sorry about that. Some of us did try. Committees don't work well if there isn't sustained consensus over a long period of time.
9
u/SkoomaDentist Antimodern C++, Embedded, Audio Feb 13 '23
The more I know about C++ and hear about the committee proceedings, the more clear it appears to me that most of the committee members should be forbidden from working on anything software related.
4
u/14ned LLFIO & Outcome author | Committee WG14 Feb 13 '23
Committees are good when there is widespread consensus, okay when something is so niche few understand it and everybody else agrees they are ignorant, and poor when everybody thinks their way is the only way.
In multinational corporations we have this thing called "management" who makes that latter situation work well. Because WG21 has no management which can impose its will and nobody else gets a choice (i.e. ability to hire and fire), we aren't good at that latter, and I don't know if it's solvable.
Be aware no other programming language is any better, unless it is directly controlled by a multinational with management to force a decision on no-consensus design issues.
2
u/Untelo Feb 13 '23
but it won't include me.
Is this only wrt. networking specifically, or have you personally given up on standardising any kind of low level I/O primitives?
3
u/14ned LLFIO & Outcome author | Committee WG14 Feb 13 '23
Networking only. The committee is warm to very warm on the direction and travel of
file_handleandmapped_file_handle, and so long as it stays that way, I'll stay to the course.If at a later point the committee decides that they actually want to deliver Networking and there is consensus on a bare minimum viable delivery vehicle, I may return to the fray, if I think that bare minimum viable delivery vehicle is something I could be useful working upon.
No bare minimum viable delivery vehicle equals no participation from me. People noted I didn't even turn up in LEWG for the review of Dietmar's paper, and that was deliberate - I'm not going to add even more to the kitchen sinking. We need less, not more kitchen sinking here. The committee needs to decide if it actually wants to ship Networking or not.
1
u/Untelo Feb 14 '23
Concerning the file I/O, I saw that the latest proposal did not feature any asynchrony. If it is delivered as is, the fixed ABI would mean that asynchrony can no longer be easily added later, would it not?
2
u/14ned LLFIO & Outcome author | Committee WG14 Feb 14 '23
Async file i/o is slower than sync file i/o for cached files on average.
Only if you disable caching (i.e.
O_DIRECT) can async file i/o substantially improve on sync file i/o, and even then, if you're on a fast NVMe SSD which responds within a microsecond async will be slower than sync, as the async ceremony is not free of cost.Async non-cached file i/o especially shines on spinning rust and networked file systems, but otherwise, it's usually a negative.
One of the things a majority in the room when I presented my BMV networking at WG21 last week clearly didn't understand is that you can swap increased latency for more concurrency, but you can't swap less concurrency for reduced latency. Therefore, the correct design approach for networking is to focus on minimum possible latencies first, and only thereafter should one add on top concurrency.
Unfortunately, most in that room thought "more concurrency" ~= "more performance" and I failed to teach them more accurate understanding in the time I had.
There is no issue building an async API on top of a fixed ABI non-blocking API. In fact, that's Linux
epolland nobody is complaining much about it. Obviously for platforms with true async APIs internally we'd redirect the sync API into an async based emulation on a per-handle basis if the end user specifically asks for a sync API to be plugged into an i/o multiplexer, and that lets you plug existing sync based code into your async framework of choice without recompilation. All that worked fine on my proposed networking, indeed mine has a coroutine based API which proves it works just fine on async. The implementation matched or exceeded ASIO's performance, so it's no slouch either.Key point I'm making here is we can't standardise a kitchen sink networking. We need to work in bite sized chunks digestible by the committee's very limited time and capacity. That equals nobody getting what they want initially.
2
u/Occase Boost.Redis Feb 14 '23
14ned
I don't even think the unicorns are the biggest problem. S/R seems to unsuitable as the basis of network facing code since they suffer from unfairness, starvation and stack-overflow in some scenarios, as discussed here
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2469r0.pdf
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2480r0.pdf
Chris K has decades of experience writing network servers, Eric and his NVidia team seems to have none, so why do they think their model is good for a problem domain they know nothing about? This is very concerning IMO.
It concerns me even more when I see that Ville changed his mind in a week or so and wrote paper to back his decision agains net ts that looks just a rant.
→ More replies (0)6
u/Occase Boost.Redis Feb 12 '23
No chance, senders and receivers are what might eventually and unfairly make in to c++26.
3
3
u/pjmlp Feb 12 '23
By then, C++ will only matter for brownfield development, unless current issues with language evolution get sorted out.
32
u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 11 '23
I'll update this with the 15 or 20 additional things, but here's where we were recently.. https://github.com/steve-downey/papers/blob/master/wg21-status.org
Deducing this, consteval if, range formatting, standard debug formatting, monadic optional, and a ton of range algorithms.
C++23 is going to be much nicer for library and app programmers.
21
u/Sify007 Feb 11 '23
I see that std::expected and it's monadic interface is there. Related to that - what happened to zero-overhead deterministic exceptions that Herb Sutter championed a few years back?
43
u/dodheim Feb 12 '23
Sutter proposals only exist to give you pleasant "what if" scenarios that will never actually play out.
16
u/BrainIgnition Feb 12 '23 edited Feb 12 '23
TBF
<=>did make it in andenabled user type NTTPsmakes my code DRY.7
u/sphere991 Feb 12 '23
Class types as non-type template parameters were originally based on
<=>, but that design was problematic for a number of reasons and eventually changed.See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1907r0.html and https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1907r1.html (R0 contains rationale and design, R1 with the wording that's the C++20 rule and remains today).
22
u/14ned LLFIO & Outcome author | Committee WG14 Feb 12 '23
It's blocked awaiting a compiler implementation. Which anybody here could also supply.
14
u/Sify007 Feb 12 '23
Ah... so it's missing the proof of concept to move forward.
Thank you very much!
34
u/serg06 Feb 11 '23
What are these revolutionary new features that C++23 brings?
I took a look at the features list, but there's no option to sort by Revolutionary.
28
u/LdShade Feb 12 '23
"Deducing this" means no more need to have a const and non const copy of functions, finally that hole in the language is patched up. (Also allows everyone to get the safety bonus of ref qualifiers without specifying four copies of a function which no one bothered with)
Furthermore, "this" no longer has to use pointer syntax and can even be passed by value for performance boosts.
Perfect forwarding out of lambdas and easy recursive lambdas are now supported.
CRTP no longer has to have the ridiculous recursive template parameter, it can now just be a normal class that you inherit.
Probably one of the most major features to come out in recent times. https://devblogs.microsoft.com/cppblog/cpp23-deducing-this/
13
u/equeim Feb 12 '23
"Deducing this" means no more need to have a const and non const copy of functions
At the cost of making them a templates. Sure it doesn't matter if the class or this function is already a template but not everyone's code consists 100% of templates.
14
u/LdShade Feb 12 '23
What's the objection with turning two copies of a function into a template that is only ever instantiated twice?
The only reason it uses the template syntax is because it's piggybacking on the existing type deduction in the language, no other reason, they essentially don't function like templates in most of the usual ways unless you're using it for CRTP in which case you already had templates.
4
u/jonesmz Feb 13 '23
I can say without a shred of doubt (because I measured...), template over-use in my work codebase is a huge source of compile-time bloat.
I'm slowly de-templatifying code as much as I can, and instead trying to rely on strategically designed implicit conversions where possible, and it's helping, a lot.
E.g. if you have a comparison operator between
FooandBar, but under the hood it's just doing a string comparison , why not allow Foo to implicitly convert tostd::string_viewand compare with that, instead of template-ing on the type (which might be aFoo, might be aBar, might beString, so on) ? I've been doing this in my codebase and it's showing promise at reducing compile times and reducing the cognitive overhead for the developer.While Deducing This is a god-send for things that are already templates, its a major misdesign for things that are not templates, and I'm very reluctant to see it introduced into non-template types in my codebase.
You might say "But Modules will make that not a problem?", and I'll point out that Modules does not work in any current compiler release, and no build tool that builds C++ supports modules in a non-experimental fashion. We'll get Deducing This long before we get Modules in most of the industry.
4
u/tvaneerd C++ Committee, lockfree, PostModernCpp Feb 13 '23
Implicit conversions are typically considered bad unless, for example, the Foo really is semantically equivalent to a string.
2
u/equeim Feb 13 '23
Also modules do nothing to help with this (that's not what they are for). They don't change existing model when each translation unit is compiled separately. Sure, they allow compiler to store source code of class definitions / function declarations in a more optimized way instead of just text, instead of parsing them over and over. However templates will still have to be instantiated independently for each translation unit, just as before.
1
10
u/equeim Feb 12 '23
It's not going to be instantiated twice, it's going to be instantiated every time it is used with different template parameters, separately for each translation unit. That may range from dozens to thousands of times depending on the size of the project. People that are smarter than me say that template instantiations are the real reason behind C++ compile times.
Also templates break IDE autocompletion since types of objects are not known in function definition. I still use them of course but I try to be conservative about it.
13
u/LdShade Feb 12 '23
every time it is used with different template parameter
This is the point, the only different template parameters are non const class, const class (rvalue and lvalue if you also count ref qualifiers). This is not like a normal template function which can take anything that has a certain interface, there's only 2 (at most 4) options.
templates break IDE autocompletion since types of objects are not known in function definition
Again, this won't apply because the IDE can easily know the type because the type is always "this", it's never going to be an int or bool or random class, the only thing it won't know is the cvref qualifiers which shouldn't affect auto completion.
It's not templating over types, it's templating over cvref qualifers (again, unless you are using it for CRTP).
2
u/equeim Feb 12 '23
This is the point, the only different template parameters are non const class, const class (rvalue and lvalue if you also count ref qualifiers). This is not like a normal template function which can take anything that has a certain interface, there's only 2 (at most 4) options.
It would still happen independently for each translation unit in which template is used. If this is an utility class that is used widely in a codebase that would result in a lot of instantiations. Though I suppose it is possible to explicitly instantiate it in its own translation unit (at least for type it's member of) but then it would still be a mouthful of code to write.
Again, this won't apply because the IDE can easily know the type because the type is always "this", it's never going to be an int or bool or random class, the only thing it won't know is the cvref qualifiers which shouldn't affect auto completion.
It also can be a subclass of "this". This may not be a problem for autocompletion, but it would still require special support from IDE or clangd (if IDE uses it which many do. And btw clangd devs has a lot of work ahead of them including modules).
0
u/jonesmz Feb 13 '23
Though I suppose it is possible to explicitly instantiate it in its own translation unit (at least for type it's member of) but then it would still be a mouthful of code to write.
Extern templates are absolutely a useful technique for cutting down on compile times.
But it seems rather silly that the language gives us a feature which we have to work-around in this way. Yes, it saves us on typing in the implementation of the function(s) in question, but is
// header file class Foo { deducing this signature that i can't remember { } }; extern Foo::deducing this signature that i can't remember with specific instantiation; extern Foo::deducing this signature that i can't remember with specific instantiation; extern Foo::deducing this signature that i can't remember with specific instantiation; extern Foo::deducing this signature that i can't remember with specific instantiation; // source file Foo::deducing this signature that i can't remember with specific instantiation; Foo::deducing this signature that i can't remember with specific instantiation; Foo::deducing this signature that i can't remember with specific instantiation; Foo::deducing this signature that i can't remember with specific instantiation;really better than just copy-pasting the function body, if you don't want to see literally 10,000 instantiations of this function? I'm not exaggerating. Out of the large codebases out there, mine is on the smaller side, and we legit have well over this number of translation units that get compiled per build, almost all of them referencing a core set of header files that are prime candidates for Deducing This.
And yes, maybe Modules will be the solution to this, if they're ever available for use in the wild, and I can manage to teach my co-workers how to deal with the insane syntax choices that they came with.
1
u/jonesmz Feb 13 '23
See my comment reply that's adjacent to yours (e.g. go one level up) for confirmation that template instantiations are slow in my experience, and what I've been doing to try to cut down on them.
Link, for the lazy: https://old.reddit.com/r/cpp/comments/10zucok/c23_hits_the_road/j8b69u9/
3
u/Jannik2099 Feb 12 '23
What's the objection with turning two copies of a function into a template
Because now you have to put the function definition in the header.
3
12
6
u/EinZweiFeuerwehr Feb 12 '23
Sadly, heterogeneous lookup in associative containers remains mostly unusable in C++23 because P2363 wasn't finished in time. That means basic stuff like at() or [] still doesn't work.
4
u/azswcowboy Feb 12 '23
True, but the paper has been approved by the working groups for c++26 â it canât be moved forward to the working draft until c++23 is gone because there standard document doesnât support branches. It should move into the 26 draft at the next meeting, so implementations may appear sooner than later.
15
u/Adequat91 Feb 11 '23
From my POV, c++17 and c++20 were much more appealing at their time.
18
u/catcat202X Feb 11 '23
C++23 isn't as large of an update, but the temporary-type traits and derived-type deduction are huge improvements to the abstractions we can build. I have many refactors for my code already in mind.
16
u/BenFrantzDale Feb 11 '23
Donât forget
std::ranges::to<Container>(range)!3
u/maxjmartin Feb 11 '23 edited Feb 13 '23
I just read a Tour of C++20 and was very surprised and impressed by them. I was like why werenât those around long ago! But Iâm glad they are beat now.
*edited typo on the version covered by the book. đ
1
u/hmich ReSharper C++ Dev Feb 12 '23
What's "a tour of C++23"?
1
u/maxjmartin Feb 12 '23
A book.
1
1
u/BenFrantzDale Feb 13 '23
I suspect the answer, as usual, is because thereâs a balance between âgood enoughâ and âitâll be part of the standard foreverâ. I want nice things now, but until we have a way to deprecate things, we need to make sure things are solid before they go in.
2
u/catcat202X Feb 15 '23
Features do get deprecated. They deprecated implicitly declared copy-assignment operators in the presence of a default copy constructor or vice-versa, and they deprecated volatile compound assignment operators. They've even deprecated library features like
std::iteratorandstd::auto_ptr.
4
3
u/dwr90 Feb 12 '23
Any chance for pattern matching im C++ 23 still?
12
0
Feb 12 '23
[deleted]
2
u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 13 '23
Clarifying further, the shape of C++23 was done two meetings ago, the last two meetings were just to address bug fixes in the features already included.
1
u/dwr90 Feb 12 '23
Thanks for the clarification, I wasnât up to date with the fact that the Issaquah meeting had already happened and that therefore C++23 had already been finalised. Just saw an article about that this morning
1
u/14ned LLFIO & Outcome author | Committee WG14 Feb 13 '23
There is a photo taken of the committee whenever we ship a standard. I am surprised the one from Issaquah hasn't appeared here on /r/cpp yet actually.
2
u/dwr90 Feb 13 '23
I'm surprised as well, I can't actually find much on the meeting at all, except for this blog series
2
u/exodusTay Feb 12 '23
So we are still using C++17(actually switched to it pretty recently), does it make sense to switch to 20? last time i checked modules were shaky at best. I want to switch to it just to use format and ranges but i dont know if changing C++ versions would cause problems.
15
u/STL MSVC STL Dev Feb 12 '23
Yeah, it makes sense. All of the major implementations offer compatibility between 17 and 20, and migration shouldn't be too difficult.
Modules in particular are still receiving a steady stream of compiler fixes, but you can use everything else with classic includes. For modules in MSVC, I can give you my impression of the state of things - I am closely tracking all of the compiler bugs that are known to affect the STL. In VS 2022 17.5 which will be released soon (can't say when but it's up to Preview 6 right now), I think the compiler's support for C++20 header units and C++20 named modules, also C++20 Standard Library Header Units and the newly added C++23 Standard Library Modules, will be suitable for early adopters who are prepared to deal with compiler bugs and rough edges (especially parts outside the core toolset, like IntelliSense and build systems). Like, a lot of stuff works - we have ongoing test coverage to verify this - but the language has lots of complex corners and we're still finding things to fix. There are a whole bunch of fixes queued up for the VS 2022 17.6 release, some in Preview 1 and many more in Preview 2. My hope is that 17.6 will be the release where things are really solid and we have fewer asterisks to report.
It would be nice, of course, if we could simply say that boom, everything totally works with crystalline perfection in a particular release. For many features, we can come close to that ideal. But modules are a total rethink of how the compiler processes C++, saving and reloading it in a structured format (instead of the messy memory dumps that are PCHes), and this involves tons of extremely complicated interactions with all of the various parts of the Core Language and Standard Library. We know people are excited to try modules (the build throughput is bananas) and we're making progress with each release thanks to early adopters who have the time and energy to put up with a less-than-perfect experience, paving the way for others to have an easier time.
4
u/Alandovos Feb 12 '23
Quick plug to say that named modules are where it's at! I'd characterize header units as just starting their optimization work.
8
u/STL MSVC STL Dev Feb 12 '23
You're totally right. Named modules have better throughput (as indicated by their significantly smaller size on disk too) and better cleanliness, and are also simpler to build. The only advantages of header units are that it's possible to migrate a codebase with minimal to no code changes and that they work for the Standard Library in C++20 mode.
5
u/Alandovos Feb 12 '23
We're in violent agreement! I meant my post as an FYI for anyone else looking at header units because the build speed improvements aren't bananas yet. Header units in msvc are still, as you said, for "early adopters who have the time and energy to put up with a less-than-perfect experience".
Header unit perf will come soon! Top devs are on it! Top devs.
2
u/pjmlp Feb 13 '23
I would already be happy when the endless set of macro redefinition warnings on Windows SDKs (specially from SAL) goes away.
Thanks for the work fixing existing issues.
7
u/jonesmz Feb 13 '23 edited Feb 13 '23
Ignore modules. They are literally useless for you until all the compilers / build tools your company uses are fully ready to support them. Revisit Modules in, e.g. 2025-2026, after you've updated to C++23. By then most of your stuff will support modules and you can start playing with them.
C++20, in my opinion, has 2 main features that should be, by themselves, all the justification you need to update your toolchain:
Space ship operator is going to let you eliminate a huge amount of boilerplate in your codebase.
C++20 Concepts, despite some slightly surprising Rough Edges, are also fantastic and a substantial improvement over SFINAE in anything more complicated than a trivial comparison.
There's obviously other features, like you mentioned, that are also worth paying attention to in their own right, but I'm only talking about the two things that, for my work, revolutionized things for me, namely operator<=> and C++20 Concepts.
So far the only three issues I've had with C++20 in my codebase were:
MSVC (and I assume the others) implicitly turn on the
/utf-8flag, which treats your C++ files as if they were UTF8 files. This is a good improvement, but it immediately breaks some weird shit if you're rocking C++ files originally authored in the stone age. Just turn it off at first, and come back to it later.C++20 comes with the
char8_tdata type. This is great, and I love it, but it breaks all sorts of stuff if you have a bunch of string handling template functions. Just turn it off at first, and come back to it later.operator<=> introduces some new restrictions on commutative comparison operations. E.g. if you define the comparison functions for two types, in both types, you have an ambiguity in how to do the comparisons that C++20 won't like.
The most effectively solution to the commutative comparison ambiguity that I've found is this heuristic:
For any type that your compiler bitches about, replace all of the comparison functions with operator<=> and operator==
Any type you use operator<=> in, change from member-function comparison operators to hidden friend function comparisons.
When you implement your hidden friend operator<=>, always implement it with the "type" that the hidden friend is living inside of as the LHS argument, and then the type you're comparing against as the RHS argument.
Make sure to implement operator== as well, since a non-default operator<=> means no implicitly defined operator==.
E.g. if you have a class like
class MyWeirdString { bool operator<(MyWeirdString const&); bool operator>(MyWeirdString const&); bool operator==(MyWeirdString const&); ... bool operator<(std::string const&); bool operator>(std::string const&); bool operator==(std::string const&); }Then you'd want to change it to
class MyWeirdString { private: // hidden friend idiom std::strong_ordering operator<=>(MyWeirdString const&, MyWeirdString const&); bool operator==(MyWeirdString const&, MyWeirdString const&); ... std::strong_ordering operator<=>(MyWeirdString const&, std::string_view); bool operator==(MyWeirdString const&, std::string_view); }And you'll also want to try to reduce the amount of comparisons you have per type, overall, in favor of relying on implicit conversions when possible. E.g. if you're comparing
std::stringas well asMyOtherWeirdStringdirectly, try to instead compare againststd::string_view, and add implicit conversion operator fromMyOtherWeirdStringtostd::string_view, and omit a comparison betweenMyWeirdStringandMyOtherWeirdStringif you can manage to.This is the pattern that I've found to involve the least amount of bull-shittery with converting my work codebase to C++20, where the comparison operators come into it. Adopting this pattern has worked in 100% of the 30-ish places where we had the compiler bitching about ambiguous comparison operators, and I expect to see the hidden friend idiom help with compile times (a small amount, not a huge amount) when we adopt it for other types as we do maintenance, because it reduces the lookup cost of argument-dependent-lookup when calling comparison operators, helping the compiler omit non-candidates much faster.
4
u/STL MSVC STL Dev Feb 13 '23
We recently converted MSVC's STL to use hidden friends as much as possible (we'd use them even more if the Standard didn't depict many operators as free functions), they're great. As a bonus, they make it easier to modularize the code later. (Free functions have to be individually exported, but hidden friends don't - exporting the top-level class is sufficient.)
1
u/jonesmz Feb 13 '23
we'd use them even more if the Standard didn't depict many operators as free functions
Could you elaborate a little bit on this?
Is this to specifically enable something? or just wording that was later understood to be less than ideal, but changing it may break some codebase in bizarre ways?
1
u/STL MSVC STL Dev Feb 13 '23
It's the latter, as I understand it - nobody thought it was a big deal back in the C++98 era, but it can be observable (typically for code with weird conversion functions). It would probably be best to just friend 'em all since it's not that big of a breaking change - MSVC has actually done so for the
<system_error>operators and we've gotten away with it.1
u/jonesmz Feb 13 '23
Thank you much.
I suppose I don't see a reason not to make such a minor change, even with the small risk of a minor break.
Hopefully it happens eventually.
Cheers
1
u/TemplateRex Feb 13 '23
Is it recommended to also make
swap,empty,sizeand non-memberbegin/enditerators hidden friends?1
u/STL MSVC STL Dev Feb 13 '23
empty, size, begin, and end should just be ordinary members; the Standard's non-member functions will handle calling them.
swap is weird. I suppose both a member and a hidden friend is the best technique but things have changed over the years so I don't know if that's fully accurate. (I am in a weird position where I don't actually write a lot of C++ as an ordinary user; most of the time I just do what the Standard tells me to do.)
5
u/wyrn Feb 12 '23
I'm still in the early stages of the switch from 17 to 20 and I'd say it's already been worth it. Designated initializers alone are a game changer, even with the caveats.
2
u/azswcowboy Feb 12 '23
17 to 20 wasnât a hurdle in our codebase, but ymmv. Modules implementations arenât really there yet on a couple platforms, but theyâre getting there. To get the full power of format and ranges you need c++23. For format I expect to be using libfmt for another year until gcc13 is fully baked.
2
u/wh1t3lord Feb 12 '23 edited Feb 13 '23
I hope they will implement compile serialization in C++23 at least, because I expected that in C++20 :/
1
u/xorbe Apr 20 '23
When does g++ typically gain all the features of a new standard. Seems like a couple key ones for 23 are still missing on the gcc status page.
0
Feb 12 '23
[deleted]
3
u/jrmwng Feb 12 '23
You may be interested in transaction memory. I encountered it with Intel's "_xbegin(), ..." intrinsic functions a few years ago.
3
u/jonesmz Feb 13 '23
So I've tried to read the paper for byte-wise memcpy several times, and fail to understand both what it does, and how it achieves it.
Could you explain it to me as if i were a guy who uses
std::atomicto implement reference counting pointers, but don't have the low level CPU hardware knowledge needed to fully unlock the power?
172
u/RowYourUpboat Feb 11 '23
New features are listed here: https://en.wikipedia.org/wiki/C++23
Why does everything have to be a YouTube video these days?