85 points · by agluszak · 3y ago · 138 comments
Hidden gem!
On a more serious note, the author of this little proposal (officializing "_" as a no name placeholder) had to perform a thorough research to show that this change would not break existing code.
This kind of attention is necessary for a language with broad scope and a long successful history such as C++, and would be for every other language with such characteristics.
While more modern languages benefit from the errors of the older ones, I do not think they will be exempt from this kind of responsible growth process when they will become decades old.
This is true, but C++ hasn't learned enough from other languages' experiences either. Let's assume that `_` couldn't not be introduced without breaking changes. Even in that case it would be reasonable to opt in that syntax for some blocks of code, without breaking any existing code which hasn't opted in, and this "edition" mechanism was very successful to evolve syntaxes and often semantics in recent languages (JS, Rust, ...). C++ is still struggling with a false premise of stable ABI (see [1] for a good argument against it).
[1] https://thephd.dev/binary-banshees-digital-demons-abi-c-c++-...
You can't really do this in a language where the way you use libraries is by textual inclusion of headers which can include arbitrary code (no one uses modules).
It would have been extremely difficult to do this for say C++ 20 even if the other work was abandoned (which IMO would have been sensible) and I feel confident it is now impossible.
One possible design is to assign editions to all preprocessed tokens. This implies the edition definition should be some sort of pragma (say, `#pragma STDCXX EDITION 2023`) and preprocessors should add pragmas themselves if tokens with a different edition are transcluded into other tokens. Implementations with integrated preprocessor and parser may also use a simpler internal representation to convey editions as well. Now `_` would be a normal identifier in past editions and a placeholder in a new edition, however they are passed through macros. For example:
#pragma STDCXX EDITION 2023
// Hereafter `_` is parsed as `_2023` internally
#define GUARD() auto _ = make_guard()
#pragma STDCXX EDITION 2020
// Hereafter `_` is parsed as `_2020` internally
{
GUARD();
GUARD(); // Okay, equivalent to `auto _2023 = make_guard();`
foo(&_); // Up to the proposal author, but P2169 suggests that
// this `_` should refer to the second `_2023`
}
auto _ = make_guard(); // Okay, equivalent to `auto _2020 = make_guard();`
auto _ = make_guard(); // Nope, `_2020` can't be defined more than once in the same scope
Of course there are more questions to answer. For example what would `#` and `##` do to editions? Should the current edition reset after `#include` finishes? (I think so.) How would we compute the edition for specific syntax from constituent tokens? (A systematic approach would be to collect a set of all editions used in syntactic tokens, and if not all editions in that set result in the same result, to issue an error.)But I still argue that this design is simple, checks all checkboxes as long as open questions are decided, and can be independently deployed. And this token-level tracking is not something alien, it's required to give more accurate diagnostics anyway---and other languages like Rust have even used it to implement things like macro hygiene.
(content warning: below are just some thoughts without any kind of final point or argument; skip if you're not interested in random musings)
Makes me think of evolution, biological death, and the limits of lessons learned. C++[0] embeds decades of practical experience; many mistakes and pivots add up to how it looks today - but every change made meant complexity was growing, and today, it's hard to incorporate new experience into the language, because of all that accumulated complexity. New languages get to distill what works, and incorporate the lessons into their design directly - starting fresh, without the complexity baggage. However, some things are lost in translation - all that complexity thrown away wasn't just random noise, it was a record of mistakes and wrong turns that the new languages are now liable to repeat.
What I'm thinking is: evolving a language for decades keeps all the experience, but becomes superlinearly[1] expensive and slow. Starting from scratch resets the costs, but also loses some experience in the process. Where continuous evolution of a language, in the limit, will be approaching the point where incremental upgrade takes infinite effort to pay the "complexity debt", the "one funeral at a time" evolution seems like it could reach a different limit: the point at which it each generational transition loses as much experience as the subsequent generation will add.
Not sure what to make of it yet.
--
[0] - I'm including here both the language proper, standard library and compilers, which all co-evolved, and all form the larger body of C++ as a real tool, and not just paper abstraction.
[1] - Would say exponentially, but I'm not really sure how fast it grows - only that it grows faster than linear.
In principle. In practice, most new languages distill _some_ of what works and incorporate _some_ lessons. Which is ok, but the point is that when they hit those situations where things _don't_ work, they either ignore it or start becoming more complex themselves.
> but becomes superlinearly[1] expensive and slow
Are you talking about the language standard? The standard library API? Idiomatic use "cognitive load"? Advanced use brain load?
For some of these, your statement is not true. Especially for the idiomatic use of the language: Quite a few things have gotten simpler, rather than more complex, for the lay user, with the addition of more functionality into the language.
> Are you talking about the language standard? (...)
I'm talking about effort to incorporate new lessons into language standard, and/or standard library, and/or compilers, while trying to maintain some backwards compatibility with at least few earlier "versions", and generally not reinvent half the language. This means you can't just add or modify a thing - you'll need to do some serious research, discuss things with compiler vendors and interest groups, etc. And you usually can't implement the new idea directly in the most clean/obvious way - you need to find a way that doesn't interfere with any of the existing rules of the language and surrounding customs.
I think that may be the motivation for Sutter's work on cpp2.
At least, I interpret it as saying there isn't much publicly available Rust code, and only a few places to find Rust code.
I have a hard time even estimating how long it would take to test a change against all publicly available C++ code.
FWIW, the C++ standards developers use do use code search tools to help identify possible breakage.
For C++, there's no registry to start with, but even if there was: there's no standard way of building and testing projects. So, a crater build like this is simply impossible at a scale.
It is called D.
> most code will start running much more slowly
Most code, as in, most code that uses it? Are there are stats on how exactly std::map (and std::unordered_map, the more useful general purpose map) are used?
std::unordered_map is a hash table. It's not a very good hash table but that's what it is, and so whilst that is much more generally useful, it's also irrelevant to the purpose of map (and thus of flat_map).
It seems to me that if "most code" would be harmed by using flat_map instead of map that's actually good news, since map isn't going anywhere. What I anticipate is the reality is that a considerable amount of code would benefit from using flat_map instead of map, because of the improved performance from linear access.
I said for latency, not performance in general. If you're generally working with maps with only 10s to 100s of small entries, it's easily possible to see a 10x reduction in worst-cost latency by switching from std::[unordered_]map to flat_map, due to a massive reduction in latency and pointer chasing (e.g. std::unordered_map allocates for every single insertion, and std::map lookup/pointer chasing often leads to log(n) cache misses).
Maybe next year ..
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p08...
P2530 hazaerd pointers https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p25...
P2757 type checking for std::format https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p27...
This is one of a pair of proposals (the other is for RCU) - both of which will likely get accepted - with useful but simplified reclamation schemes. In Rust their equivalents live on crates.io, e.g. https://crates.io/crates/haphazard is more or less the same Hazard Pointers, even based on the same work at Folly - and so they can be updated, or obsoleted, or alternatives may become prominent, it all Just Works™.
Closer to safety work is the proposal to have C++ admit that it's possible for a program to be wrong and yet not throw its hands in the air and refuse to explain what it does, as "Erroneous Behaviour". The idea is that e.g. suppose I declare there's going to be a variable named foo, which is an integer, but then I never actually initialize it. Well that's not OK, that program is nonsense, but C++ has long allowed it, even though it's nonsense, so just refusing to compile would annoy programmers. On the other hand, if foo is now silently zero, we actually introduced new semantics to the language, which isn't OK. With "Erroneous Behaviour" C++ could emit a warning ("Don't do that, uninitialized variable foo") but also zero it anyway. A new syntax would be added to mean "I genuinely don't want this initialized, for some good reason" because it's still C++ so being needlessly dangerous is part of their whole ethos, but at least you did it on purpose.
I tried to find this proposal but the links don't work, once upon a time it was 2795 "Erroneous Behaviour". Maybe the author asked that it be withdrawn for some reason?
Can't the new-feature enthusiasts just make a new language, or join existing efforts at building new ones? C++ is already a mess of around 3.5 languages jumbled together. When will it stop?
Other than catering to your unexplainable desire to see the death if one of the most popular programming languages ever designed, what would be the point of that?
More importantly, what leads you to believe that there is value in stopping others from improving upon a language they use?
> Can't the new-feature enthusiasts just make a new language, or join existing efforts at building new ones?
What's the point of that? You have a perfectly good and working programming language that is used to build software everyone in the world uses every single day. Why would anyone think it's a good idea to just throw that out and water their time reinventing the wheel? Are you the one who is going to rewrite every single C++ software project into your flavor of the month?
Continuously "improving" a language has upsides and downsides. The upside is, of course, people get to use the new features, which is good if the features are useful. A significant downside is the added complexity once there are very many features. A lot of the features of C++ don't interact in good ways and generally make it easy to shoot oneself im the foot. C++ has bad defaults (they were most reasonable at the time, of course) and it's not possible to change them due to backwards compatibility. C++ is also the most complex (widely used) language, to the point where it's quite easy to produce a 20 line program that half the ISO committee couldn't tell you if it should compile or not (they do this apparently for fun sometimes in Cpp-con talks).
To summarize, the point of stopping to add features is to have a stable language that is manageable. It will be around for many decades and not be thrown out. However, if it keeps evolving at the current pace, any new developer inheriting a large codebase will have to know 10 programming languages crammed into one and interacting in weird ways. If your answer is "what's wrong with that?", I guess we won't agree.
Question for you: Should they start adding variadic templates and mutable lambdas to C? Would you like that?
> Are you the one who is going to rewrite every single C++ software project into your flavor of the month?
Not wanting to rewrite code is the very reason they should stop adding features. Already, the standard suggestion in C++ circles is that you should "modernize" your codebase and use "best practices" that change every decade. That's bordering on rewriting it in a different language. I am against rewriting stuff that works in a new language. The sad thing is that I agree with pretty much all these "modernization" suggestions individually. The bigger picture of it seems crazy though.
New projects can be started in new languages that don't need to be backwards compatible to stuff from the 80s and have sane defaults with the benefit of hindsight. That's how languages evolve in my view, sometimes starting fresh is the way to go. The old languages stay around for a long time, which is fine. Incessantly messing with them and making them more complicated is the issue.
It has zero downsides. If you are interested in new features you're free to adopt them. If you are not interested in them then you can simply not use them, or even sit out a specific version. There are zero drawbacks.
> A significant downside is the added complexity once there are very many features.
There is zero complexity. No one forces you to play standard library bingo. No one forces you to use each and every single feature ever devised. No one forces you to adopt a new standard.
That's the best feature of C++'s standardization process: it specifies existing features, and provides you with a convenient way to onboard or not onto them.
I've worked on C++14 projects until a couple of years ago. My team inherited a C++11 project and we decided to migrate to C++14 mainly because of std::make_unique. We considered C++17 but we didn't bothered with it. C++20 was already out. No one held a gun to our heads. It's ok if you don't jump onto the latest and greatest.
> Question for you: Should they start adding variadic templates and mutable lambdas to C? Would you like that?
C would greatly benefit from supporting templates. I don't understand what point you tried to make. The question sounds too luddite.
> Not wanting to rewrite code is the very reason they should stop adding features.
This is a pretty lame strawman. No one is forced to rewrite stuff when bumping a C++ version. I was involved in a couple of projects that underwent those upgrades and basically the migration consisted of flipping a switch in the build system and addressing a couple of compiler errors caused by the compiler flaggin a couple of warnings as errors. That was it.
Things are simpler if we don't invent problems.
Who knows what might still come into C26.
Some big new C++ features reflect this reality, for example coroutines: it's not a feature you would use directly as an application developer, but it provides sufficiently flexible underpinnings that allow these FAANG teams (and other library developers of course) to integrate coroutines into their existing async libraries and start converting code over.
That's a bit sad. It's a beautiful city, in places - the older buildings (neglected), the waterfront park that stretches KILOMETERS, one of the best puppet museums in Europe, and an incredible nature reserve parallel to the beach, out of town to the north...
btw there's much-much more (unattended) forest+small-beaches to the south than to north.. and i have a few spare tents :)
https://svilendobrev.com/snimki/more/
That said.. very little software/tech stuff happens here, all goes to Sofia :( and people follow..
* Standardizing `__restrict__` (which C has, and C++ doesn't). Everybody relies on the compilers offering it as an extension, as without it - performance often suffers very badly.
* Universal call syntax (i.e. equivalence of `foo(myobj, myparam)` and `myobj.foo(myparam)` ). There were what I considered to be rather trivial objections, last decade, then it somehow went away.
No. Without aliasing guarantees, every load via a by-ref/by-addr input param after a store via another such param must actually be executed. So either you manually cache your accesses, or you can't do loop unrolling, strength reductions, and possibly other optimization work.
> Ultimately the only cost to check is an integer add and an integer comparison
1. Manually checking that will likely not affect the compiler. Again, you can implement your own optimizations, but that's not what you want to do.
2. The interest is also for the _definition_ side of things, not just for the implementation. I want to tell the user of my function "pass unaliased arguments". This is important, because otherwise you have to provide a bunch of convoluted implementations for the case of there _being_ aliasing - which often changes the semantics of the function to something you had no intention of writing.
3. In real life, there is no guarantee that memory ranges with disjoint addresses are not aliases of the same region. There's probably no such guarantee that the standard makes, but IANALL.
> 1. Manually checking that will likely not affect the compiler. Again, you can implement your own optimizations, but that's not what you want to do.
I wasn't implying to do it manually. Compilers do it all the time.
2, I understand and would use. I also want to restrict input alignment.
3, I never knew, but compilers I use for x86 on linux seem to think it's fine to just check memory address distances.
What's the best way for someone to keep track of the progress and the conversation around a specific proposal ?
I remember liking the idea when he presented it at CppCon (https://www.youtube.com/watch?v=ARYP83yNAWk).
A lot of features aren’t fully fleshed out when they enter the standard. For example the building blocks of coroutines appeared in C++20 but the user friendly affordances aren’t there yet — but with the building blocks in place people can work on that.
Likewise a lot of features are there for library developers and aren’t needed by regular code.
So your position is sorta consistent with what’s mostly approved by the committee. Even ranges, which are exciting, I’m still waiting on to see if some things get shaken out.
"Modern" C++ was a mountain to climb for me. I think I will not even try to catch up on modules, contracts and what not. I'd rather go back to C for personal projects. I just don't have the stamina for another big C++ language change. And they add up.
* Some new features are typically added to handle very specific cases that will rarely be encountered but can't be easily handled by existing mechanisms. These are typically used by library authors to make an API clearer or easier to use, and new developers won't need to interact with them at all.
* Some new features are added to replace existing features either partially or entirely (because the old feature had issues, or a better way of handling it was found). In that case, new developers just don't need to learn the old feature at all, or if they do, they can learn it later as a more advanced technique.
I don't know a huge amount of C++, and I haven't been following Python much recently, but to use some examples from Rust:
* GATs and a lot of the newer trait features are very much in the former category. Very few Rust developers will be directly using the new tools, but library authors can use them to create easier-to-use APIs which benefit everyone.
* The `try` mechanism is often simpler to learn than all the Result/Option combinators, which drastically reduces the amount a new developer needs to know about to get started. You'll still probably need those combinators later, but you can now get started without them.
There are definitely some features that don't fit clearly into these two categories, and add a kind of overall complexity to the language. But I think a lot of people look at a big list of features and think "but that will be so much to learn", forgetting that (a) you probably don't need to learn a lot of it until you're already very advanced, and (b) a lot of it will be making other parts of the language obsolete and overall simpler.
what ways are there to learn/teach latest c++? I can only come up with conferences' recordings
No one is going to put a gun to your head and make you #include <concepts> when all you wanted was C with Classes.
Do you think that in software development circles backwards incompatible changes only happen each 3 years?
I mean, it felt like I woke up one day and couldn't read Python anymore. I believe there is a great lag between introduction of a concept/syntax/whatever and its actual use, so I can't even pinpoint a version where Python became "too much".
Typing syntax and pattern matching look pretty significant changes, but nothing else bug comes to mind. I guess ternary expressions and perhaps f-strings are new too but not very big
[1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p27...
There are 2 names of such activities, select one which you prefer: gold plating/turd polishing...
What a silly thing to say. How many other mainstream languages support writing a library for compile-time checking of format strings? Having the power to do this as a library means that you can also write similar stuff customised to your own needs when you need to, as it doesn't rely on anything hard-coded in the compiler.
It has never been a better time to depreciate such dinosaur language controlled by a small closed group of people with a combined age of thousands. It is a language for those with several million $ worth of RSU waiting to be vested.
Except those aren't small or simple or trivial or consequence-free.
Also, we're talking about standardizing current practices. You can do networking in C++ in a myriad of ways already, and none of which is standard.
you don't see the problem here?
golang/rust doesn't spend 30 years to standardize networking/fs.
Golang and rust standardized nothing.
Golang and rust just provided implementations for features. Likewise, there's already a myriad of networking/file system libraries for C++. This is not a problem. The problem is to get to an abstraction that can work across all conceivable platforms. You can hack together something, but throwaway code should not feature in an international standard. See for example how Python's fs support is riddled with gotchas and outright bugs on platforms such as Windows.
In the meantime C++ already has things like Poco, boost, Qt, etc, not to mention platform-specific SDKs. Not a problem. Never was.
Now, Rust's standard library also provides BSD sockets in std (that cheap WiFi probe presumably lacks an OS and wouldn't have sockets). That's much less obviously necessary, although it's hardly a big problem and I think if C++ had this since 1998 there would be less squabbling. But the fundamental types are vocabulary and it's silly that C++ doesn't have them in its stdlib.
That's actually a very good example. Because sooner or later, you'll find yourself ten libraries deep with a FE80::1234 that you can't use because nine of the ten thought Ipv6Addr was enough.