I would say it is also fair to say he accumulated sooo many years of thought on these problems for the specific case of C++ that I do not find it that surprising.
The problem is that he lacks, as far as I know, C++ in the trenches experience, having stayed most at academia, with exception of Blomberg, and the contribution F-35 C++ standard, which really isn't as positive as he thinks, given what happened with all the F-35 avionics software.
Most codebases are quite far from the ideal way of how C++ best practices advocate for, and most coders don't care about conferences, improvement trainings or whatever.
I do agree with Bjarne's vision especially given The Design and Evolution of C++ got released, hence why I left C behind shortly after getting introduced to C++.
Now the big question remains, who is going to implement this PDF, notice the plea for vendors to join the effort.
So far I don't see much hurry from compiler vendors into joining such effort, alone lifetimes have existed in VC++ for a decade now, and it seems I get more lifetime help from Github CoPilot than such proposals.
Wasn't the Safe C++ proposal rejected because they didn't want "viral annotations". If this [[not_invalidating]] annotation (or whatever it becomes named, cause that's a god awful name) doesn't get slapped on basically every function, I'll eat my hat.
And I guess constructors/destructors, a fundamental part of the language that have been present since it's creation, are just too hard to analyze for type safety so every C++ codebase will have code that isn't verified via profiles?
> Wasn't the Safe C++ proposal rejected because they didn't want "viral annotations"
No. The main reason was because the Safe C++ required you to write in an essentially different language to become actually safe (special pointer syntax, change of semantics etc) . The consequence of it would be fragmentation of the language, and that it would be easier for people who care that much about safety to just switch to another mature enough language like Rust with established community rather that stay in a mixed code base with increased complexity.
Stroustrup's proposal just limits the safe code to a subset of unsafe code. That's a completely different approach, and more important, this is actually feasible.
Safety profiles adds annotations to the code to provide exactly the same markup that Safe C++ provided as part of the language, but via annotations instead
There issue is that its unfortunately fundamentally impossible to provide safety in C++ without additional information. Its certainly possible to provide that in a janky way via [[not_invalidating]] and the huge number of annotations and subsets we'll require to have a fully safe language, or you can build it into the language properly
Stroustrup's proposal just limits the safe code to a subset of unsafe code
Its a subset of superset, not a subset of the current set of C++. Using safety profiles will require extensive additional new code to be added (in the form of annotations, and profile statements), as well as extensive rewrites to fit your code into something that can be correctly analysed. Neither of them avoid the problem of having to rewrite code, because safety without rewrites in C++ is unfortunately not possible
special pointer syntax, change of semantics etc
Both of these things are provably necessary to have zero overhead safety in C++ unfortunately, so profiles will have to add these via annotations. There's no way around it
> Safety profiles adds annotations to the code to provide exactly the same markup that Safe C++ provided as part of the language, but via annotations instead.... Its a subset of superset.
It's not the same.
If you remove (or ignore via macros) [[not_invalidating]], it's still a valid C++. You can compile it with any legacy C++ compiler where the profiles are not supported. Thus you can validate safety on one platform and deliver safety on multiple platforms. While with the Safe C++ you can't remove anything because you are removing the code - you need another implementation to compile your code on legacy platforms, so you end up with unsafe code there.
> Both of these things are provably necessary to have zero overhead safety in C++ unfortunately, so profiles will have to add these via annotations. There's no way around it
There is. Those annotations can be generated automatically and used in a scalable way in the whole program analysis. It's just people for some reason insist on local analysis only. Just stop limiting yourselves and you'll get new possibilities.
So you'll need to #ifdef for older compilers anyway. Profiles also absolutely do come with semantic changes as well, so compiling for a platform that does not support the profile is a huge problem
Eg one possible option talked about in the OP is defining signed overflow as a profile. If you were to write code against that behaviour, compiling for a platform that didn't support it is incorrect. Trying to make them backwards compatible against older compilers like this requires no semantic changes, which is impossible given that the purpose in many cases is to optionally define undefined behaviour
While with the Safe C++ you can't remove anything because you are removing the code - you need another implementation to compile your code on legacy platforms, so you end up with unsafe code there.
Many necessary changes to C++ cannot be made backwards compatible, eg there's absolutely no way to make the standard library safe with profiles. So just because here and there some features can be made to resemble C++ as it is currently, meaningful safety changes are going to require a new compiler whatever you do
How do you express ownership semantics while keeping the code still compile-able under old C++, with minimal annotations, no keywords, and no ABI break? Its impossible
Those annotations can be generated automatically and used in a scalable way in the whole program analysis
I do not think profiles are far enough yet to speculate on what they will eventually look like and do. There are so many contradictory statements about on what they will do and how they will work at this point in time.
I is somewhat funny though, that after decades of denying the existance of dialects of C++, we now are headed towards standardizing what sounds a lot like a "dialect construction kit" -- complete with compiler enforcement. I am looking forward to bike-shedding which profiles a project must enable or not.
>How do you express ownership semantics while keeping the code still compile-able under old C++, with minimal annotations, no keywords, and no ABI break? Its impossible
Again, wrong goal, false limitations.
The goal is not to introduce the ownership semantics (for this you have Rust). The goal is to make C++ safe (and not math proven, but just practically safe in 99% of cases, such as invalidated iterator access).
> I'd love to see an implementation of this
That's in my plans. But as I mentioned this is not a priority for me. I don't like hype-based development. Hopefully within a year I will have time to implement a GCC POC.
I'd dearly love to see any kind of practical zero overhead memory + thread safety system that isn't ownership semantics - but until someone shows a proof of concept it seems fairly unsuitable as a model for standardisation IMO
The goal is to make C++ safe (and not math proven, but just practically safe in 99% of cases, such as invalidated iterator access).
This is exactly what worries me: Governments want you to say "this program has no buffer overflows", while C++ devs need to say "I think this program has no buffer overflow" today. That is what puts preassure on C++ as a language.
Will that preassure go away when we can make the statement "I think this program has no buffer overflows" with more convidence?
If profiles are unable to address the concern of regulatory preassure and profiles are only introduced to lessen that preassure, then why spend the resources?
But of course I am just a random guy on the internet. I am sure the committee knows what it is doing.
> Governments want you to say "this program has no buffer overflows", while C++ devs need to say "I think this program has no buffer overflow" today. That is what puts preassure on C++ as a language.
Fortunately, so called "memory safe" langs like Rust, can't be used in the domain of C/C++ without the unsafe keyword. So they can't say that safety is guaranteed either. This means there can't be pressure on C++ to achieve 100% guarantees unless it's some political lobbying. And second, since Rust can't easily replace C++ (e.g. Firefox, the inventor of Rust, after 10+ years, still has only 25% of low level stuff implemented in Rust, and 75% is still C/C++). Which means govt can't practically forbid C++ in foreseeable future, so C++ has time (probably another 5-10 years) to implement a safety model that is actually compatible with C++ design rather than rush into copying incompatible models which do not achieve 100% safety anyway.
It is C++ that has the problem with perception of its safety properties (independent of whether that perception is correct or not). Saying out loud that you want to add a few debug tools here and there and not tackle the perceived problem is not going to alter that perception.
Whether or not the competition is any better is secondary. Their perception for sure is.
Which means govt can't practically forbid C++ in foreseeable future
I do not expect C++ to ever get forbidden, there is no need to do such a drastic step. Liability law will become a problem for new projects.
C++ has time (probably another 5-10 years) to implement a safety model that is actually compatible with C++ design
By all means, implement a safety model that fits C++. Unfortunately all the models that are proven to work got rejected. So there is quite some research needed, and a lot of effort on top to turn that research into a product. I do not see such an effort pass standardisation in a decade. Not within the constraints we have right now.
> all the models that are proven to work got rejected.
It does not matter the rejected models were proven to work. They just were not a C++ (e.g only 20 or 45 EWG members supported the direction of the "Safe C++" proposal, which means >55% of members don't consider this acceptable for the evolution).
On the other side, the profiles were supported by 77% of the group even though there are no implementations. And it's not some fear of Bjarne. The committee has no problem accepting things Bjarne does not like (contracts). It's because the profiles preserve the spirit of C++ and does not limit what you can do with it.
Who is going to implement those improvements though?
Apple, Google downgraded their investment into clang development, and like everyone else seem more keen into LLVM itself as compiler tooling.
Microsoft, well it is more than known how little resources VC++ is getting nowadays, and there is the whole SFI regarding new projects.
GCC depends on heroic contributions, despite Red-Hat and IBM support.
There is a big difference knowing where the unsafe keyword is used, and having to check every single life of code, because some devs keep coding C style on C++ projects.
>Apple, Google downgraded their investment into clang development,
The example of Circle shows that you don't need a corporation to make progress.
> some devs keep coding C style on C++ projects
That is the goal of the profiles - they will be able to forbid C style except unsafe blocks. The fact that for C++ you probably won't be able to prove safety with 100% does not matter. 99% is good enough (after all, the govts have no problem convicting people based on fingerprints or other non-100% methods)
No. The point is, in Rust you can't have 100% guaranteed safety in a program as a whole, so it should not be required from C++.
Practically, C++ should just have measures against accidental unsafety, like use-after-free etc. This should cover 99% of all the current vulnerability surface.
And WRT guaranteed safe blocks - I don't care about this. It does not matter if you have math proven guarantees when you have practical safety.
So what? There are also lots of people who still program C style in C++. Their opinion is irrelevant for the C++ future. Good luck with Rust though 😄
WRT the issues in 23/26. Yes there are changes that I don't like either. But those are minor due to one major C++ feature - backward compatibility. So I'm actually excited about C++26 because it brings reflection and contracts without breaking my old code.
So what that this is already one of the most obvious problems with Cpp, so its funny to worry about it now. I mean, Ken Thompson said it literally 15 years ago
While lack of safety is indeed a quite obvious problem of C++, it's definitely not the most important.
The only reason the "safety issue" has to be addressed is because rust promoters are so loud that it became political, and govts started regulating this.
Memory safety has been the norm for like 30 years. Java is memory safe, as are C#, Perl, Python, PHP, JavaScript, OCaml, Haskell, Go, Swift… virtually all high level languages. Rust demonstrating memory safety without a GC is possible was simply the final straw.
Ah the Rust feud, I have ported several distributed applications from C++ into Java or C#, since 2006.
What happened is that cyber security became relevant, as state actors started attacking critical infrastructures, additionally companies started paying attention on the amount of money it costs in salaries, build pipelines and QA, for bug fixes related to security exploits.
However, it is possible
to eliminate access through invalid pointers through static analysis
I don't quite understand how is it achievable. Compiler may not see through all declarations. How compiler can know that that particular method is doing something nasty?
How compiler is supposed to know what is happening inside these methods?
Presumably, the same way it works in Rust: you provide enough information in the signature for the compiler to conclude that the lifetime of x is tied to not calling reset.
Are we going to practically ban dynamic libraries (like Rust)?
The borrow checker in Rust doesn't look at function bodies at all, so the less than ideal support for dynamic libraries has nothing to do with it.
Isn't it Safe C++ which was basically rejected? I was thinking that Profiles are supposed to work with no annotations...
And even here, [[lifetime(self)]] should somehow propagate through the guts of standard library to the underlying item pointer...
And what if you forgot annotate your methods? Is it going to assume what every reference that is being returned from the method is bound to the lifetime of the object?
Isn't it Safe C++ which was basically rejected? I was thinking that Profiles are supposed to work with no annotations...
Yeah, I'm curious how that circle is going to be squared too.
And even here, [[lifetime(self)]] should somehow propagate through the guts of standard library to the underlying item pointer...
Not necessarily, it's enough to mark optional<T&> as having some pointer within it. The actual internals then doesn't matter for the analysis.
And what if you forgot annotate your methods? Is it going to assume what every reference that is being returned from the method is bound to the lifetime of the object?
Most of the "complexity" in your snippet comes from constraints, not from lifetimes. I don't see how C++'s partition looks any different:
template<permutable I, sentinel_for<I> S, class Proj = identity,
indirect_unary_predicate<projected<I, Proj>> Pred>
constexpr subrange<I>
ranges::partition(I first, S last, Pred pred, Proj proj = {});
template<forward_range R, class Proj = identity,
indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
requires permutable<iterator_t<R>>
constexpr borrowed_subrange_t<R>
ranges::partition(R&& r, Pred pred, Proj proj = {});
and keep in mind that that is the documentation, where you copied the implementation (the documentation looks like this https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.partition and does not include the internal inline-defined helper functions).
But even that aside, the actual complexity is not that bad once you've learned how to read it:
again "is_false returns a function whose lifetime depends on all other lifetimes".
Keep in mind that this isn't "extra" information, it is information the caller needs anyway to understand what the function is doing. In C++ this is specified in the comments, in Rust, you put it in the signature.
Also my top comment was specifically about how I want Rust's lifetime elision, i.e. the rules when you can omit lifetime annotations enitrely.
that is transitively chaining 'a all around and there are worse examples than those.
I find Hylo much more elegant without a lot of the hassle.
Why get into that? Lightweight lifetimes for the most urgent and common cases is more than enough. Go full into this I think it would be a big mistake.
Rust lifetime elision, as you can see there, works when it works and it is easy to get kidnapped by that logic when coding and, in my opinion again, is not worth the tiny potential speedup (which by cheating the borrow checker rules in low level code anyway we can do better).
I like the Hylo model a lot myself, but it is unfortunately incompatible with the C++ standard library. You cannot have safe C++ style iterators with the Hylo model.
Everyone was saying that profiles alone would not be able to implement this. It appears we're just going to reinvent the wheel several years after inventing a perfectly good wheel already.
I look forward to the paper that proposes a new standard library that is safe.
It appears we're just going to reinvent the wheel several years after inventing a perfectly good wheel already.
The next step is realising that different disjoint safety profiles don't interact well together to create a cohesive safety structure, ie you can't linearly compose different safety profiles and end up with the sum parts of their safety
This very paper by bjarne is realising that you need significant annotations to your code to be able to implement this - we now are expected to mark up functions with [[not_invalidating]]. But as long as we simply declare that that's not an overly high annotation burden, and also make some invalid assumptions for convenience (const functions not invalidating pointers), then its all fine stop worrying about it
This is going to end up an order of magnitude more complex and annotation heavy than Safe C++
I bet that will only happen after they get into the standard, assuming they actually make it that far.
Also who is going to implement this?
Apple and Google care about LLVM, as much as it helps their own goals, Microsoft isn't giving that much resources to the VC++ team either.
Clang fronted work seems to be driven mostly by volunteers, even though most embedded companies have replaced their proprietary compilers with clang forks.
As I understand it, Rust doesn't have anything like that [[borrow]] notation in your hypothetical example. It just uses the equivalent of const qualification. That is, the reset() method would require a mut (i.e. non-const) "self" reference to the container. With (Safe) Rust, unlike C++, its mutable aliasing restrictions enforce that if you have somehow obtained a reference to an item in the container then there's no chance that you also have a mutable reference to the container itself, and thus have no way to call reset() until all references to the contents of the container have been relinquished. No further annotations are needed.
The problem with annotations is that they are limited to static enforcement, which is often too limiting to be practical, whereas with Rust's mutable aliasing restrictions you have the option of using run-time enforcement (via, for example, RefCell<>) which can be much more flexible.
But Rust's "draconian" mutable aliasing restrictions are arguably impractical for C++. Fortunately, for enforcing memory safety, Rust's restrictions are way overkill. So, for example, the scpptool-enforced safe subset of C++ (my project) imposes only a small subset of those mutable aliasing restrictions actually required for memory safety. In fact, for single-threaded scenarios, we really only need to prevent scenarios like your example where an operation on an owning (smart) pointer or container can arbitrarily end the life of owned elements.
Depending on the implementation of foo1(), this could be valid code. But it's not really amenable to reliable static verification.
In the scpptool-enforced subset of C++, the clear()/reset() issue is solved largely in the type system (also without any special code annotations, and where we can use more flexible run-time enforcement). That is, we replace the standard owning pointers and containers with ones that never return a raw reference. When you only need to retrieve or assign values of items in the container, they work just like the standard containers.
If, however, you want to obtain and hold a raw reference to an item owned by the container, then there is some extra ceremony. Specifically, you need to first instantiate a "borrowing" (or "accessing") interface to the container contents. This is very roughly analogous to borrowing a slice in Rust. The existence of that "interface" object will prevent the structure (but not the values) of the owned contents from being modified (or relocated).
This extra step is generally not a big deal. It involves some theoretical extra run-time cost, but that cost is almost never paid inside hot inner loops (and often just optimized out). So the above example ends up looking like:
mse::rsv::xslta_vector<int> vec1 = { 1, 2, 3 };
mse::rsv::xslta_vector<int> vec2 = { 4, 5, 6 };
/* We know that foo1() cannot obtain an int& from an mse::rsv::xslta_vector<int> */
//auto& int_ref1 = foo1(vec1, vec2);
/* However, it can (safely) obtain an int& from a "borrowing fixed" interface object of an mse::rsv::xslta_vector<int> */
auto bf_vec1 = mse::rsv::make_xslta_borrowing_fixed_vector(&vec1);
auto& int_ref1 = foo1(bf_vec1, vec2);
vec2.clear();
std::cout << int_ref1;
Basically one extra line of code. And it's verifiably safe in a way that isn't really doable with annotations and static analysis alone.
For those creating their own dynamic containers, the extra work is a little more substantial. You would also need to create corresponding "borrowing" and "accessing" interface types if your container interface does not conform to an existing one. But in my view this is not necessarily inappropriate. Dynamic containers, as traditionally implemented, are dangerous. I think there is some obligation to provide a safe interface while preserving maximum flexibility, even if it requires a little more code. (And at some point it'll be more likely to be an LLM doing that work, right?) So here is a quick example to give an idea of what your collection container might look like in the scpptool-enforced subset.
Anyway, the point being that your skepticism of the statement
However, it is possible to eliminate access through invalid pointers through static analysis
is justified. Rust was not shy about using static analysis and imposing heavy restrictions, and did not have to accommodate legacy code patterns, yet they still rely on the option of using run-time checks (like with RefCell<>) to make the language practical. And corresponding options exist for in C++ too, but in a way that still largely accommodates existing code patterns.
you provide enough information in the signature for the compiler to conclude that the lifetime of x is tied to not calling reset
wouldn't it have to be the other way around? By default pretty much anything could invalidate x, and you'd have to add extra information to let the compiler know that some functions won't invalidate it.
Yes, that is the more conservative approach. Either one should work though, as long as the compiler when compiling the body of your function also verifies that you didn't lie about the annotations.
Information in signature is bad choice. We can analyse all functions and get all information we need. A function signature was written by a human it may be incorrect , a code is a source of truth, so a using a signatures as an information source is error prone approach
But there is an issue with old code in TU. but it’s ok: we shouldn’t care about legacy code, it enough the code can to be compiled.
Yes, the compiler can infer the information from analyzing the body, store it in some side table, and then use it to provde correctness of code when the function body is not available.
The problem is that this can become a compatibility hazard: Changes to the implementation can lead to more constrained lifetime properties. It is better if you are forced to explicitly promise how lifetime works.
Incorrect (as in underconstrained, overconstrained is perfectly fine) promises are not an issue because the compiler can verify them.
The problem with signature that the the same work now you need to do by hands, I pereffer the compiler works instead of me (even if it takes more time). But it can to be nice set the information in a signature and speed up completion or check what you got .
While that's dynamic linking, it's missing a crucial property: the binary still depends on the implementation details of the dynamically loaded library.
Rust also supports cdylib, which does not have that issue, but in turn imposes more restrictions, like needing to use types with a stable ABI and disallowing generics.
the binary still depends on the implementation details of the dynamically loaded library.
I am not a rustacean: can you elaborate on what you mean by this? Doesn't every binary depend on at least some implementation details of the dynamic library? Even if you use libdl and have a plugin architecture, you end up relying on some implementation details.
If you want languages that have official support for dynamic linking in their specifications, including standard ABI for how dynamic libraries look like, C++ isn't one.
Neither are most of them anyway, Swift is the only native systems language with a standard ABI for dynamic linking.
All the others are managed languages and the ABI is defined via their bytecode, used to ship libraries as well.
Guarantees that usually get lost when compiling via AOT instead of JIT, where only static linking is supported, or the bare bones C ABI, which really isn't.
That is only a thing in OSes written in C and is actually the OS ABI, ISO C is also silent on that matter.
Who cares that ISO doesn't standardize the C++ ABI? All thats matter is someone does, the particular org is irrelevant, same as with C.
The three major C++ compiler vendors do follow ABI standards, and focus on ABI stability for their standard libraries. Rust does not follow an ABI standard, and does not maintain an ABI stable standard library.
Do they? Just like Rust, it only works with the same C++ compiler on both ends.
Microsoft uses COM/WinRT, Apple uses XPC, Google gRPC and Android IPC for a reason.
Speaking of Microsoft, because it is the only I know better, the only C++ dynamic library that got released as such was MFC, and even that needs C++ language extensions outside ISO, just as does C++ Builder for VCL and FireMonkey with C++ Packages, based on Delphi ABI.
it only works with the same C++ compiler on both ends
This is obviously wrong and trivially proven false, try compiling anything with clang on any platform. If clang links msvcp or libstdc++ correctly, it's linking against a C++ ABI from a different compiler
No it isn't, because you are giving a snowflake example, where Microsoft even contributed to clang support on Windows for interoperability.
I can even install clang via Visual Studio installer!
Now try the same with Clang and C++ Builder on Windows, using the old Borland compiler, before they forked clang.
Or for example the C++ frameworks from IBM and Unisys compiled with their proprietary compilers, try to link their binaries against GCC application, on the mainframe and micro OSes.
66
u/TheRealSmolt Jun 19 '26
Side note: I find it very funny that 2/3 of his references are things he wrote. I mean when you're Bjarne Stroustrup I get it, but it's still amusing.
48
u/James20k P2005R0 Jun 19 '26
Presumably we're going to name these Shmafe and Unschmafe regions
21
u/mereel Jun 20 '26
Oh boy. This isn't super confidence inspiring.
Wasn't the Safe C++ proposal rejected because they didn't want "viral annotations". If this
[[not_invalidating]]annotation (or whatever it becomes named, cause that's a god awful name) doesn't get slapped on basically every function, I'll eat my hat.And I guess constructors/destructors, a fundamental part of the language that have been present since it's creation, are just too hard to analyze for type safety so every C++ codebase will have code that isn't verified via profiles?
10
u/borzykot Jun 19 '26
I don't quite understand how is it achievable. Compiler may not see through all declarations. How compiler can know that that particular method is doing something nasty?
For instance:
How compiler is supposed to know what is happening inside these methods? Are we going to practically ban dynamic libraries (like Rust)?
2
u/andriyt Jun 20 '26
I wish this web-site supported “reader mode” on mobile browsers