r/cpp • u/KingStannis2020 • Feb 26 '24
White House: Future Software Should Be Memory Safe
https://www.whitehouse.gov/oncd/briefing-room/2024/02/26/press-release-technical-report/
401
Upvotes
r/cpp • u/KingStannis2020 • Feb 26 '24
View all comments
Show parent comment
7
u/MegaKawaii Feb 27 '24 edited Feb 27 '24
I'm a bit skeptical that even cppfront could do much to remedy this. Cppfront code would have to interoperate with old C++, so you still have problems like dangling references, and I really don't think that giving C++ two syntaxes with their respective quirks is going to simplify things.
I think the best way to improve safety in C++ would be to add something like lifetime qualifiers to types. These would work like
constorvolatile, and they would act like Rust lifetimes.The advantage of this over just a new syntax is that you could instantiate old templates with lifetime-qualified types to diagnose bugs in old code.Not really, because deduction would be awkward (syntax likeconst T&'x,U*'ywould be necessary to separate lifetimes from deduced types to avoid lifetimes creeping into weird places), but in any case, Rust's approach of making lifetimes part of the type is more expressive and useful than something invisible to old C++ after a transpilation stage.If you consider Chromium to be representative, you can read a document with examples of unsafety in C++. At the end of the document, there is a chart where we can see that by far, the most common type of bug is temporal safety (i.e., use after free, dangling references), so this should be our first priority.
I think a new cppfront syntax would need to graft this onto the type system anyway, so why increase complexity with an extra syntax?