Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

To expand on this, my understanding is: the thing that held back approval (from the national bodies, not the committee itself) was that build configurations could turn off contract checks, which run at runtime, and still be standards-compliant. They wanted library authors to be able to write checks that will actually run, regardless of the build configuration of the person compiling!

So the “fix” was to let the author mark individual contracts as always enforced at runtime, with pre<enforced>. That, in turn, means a library’s key safety checks (bounds, null, etc.) can’t be silently dropped in a “loose” build configuration, which pushes things down a safer path.

Clunky IMO, but rather reasonable as a response to Rust!

It brings things closer to a world where: if you use these “safe” libraries, your program will stop rather than run unsafely, no matter how it was built.

 help



While I don't doubt this makes C++ safer, it really has nothing to do with memory safety which is usually understood as a language-level guarantee, either through the type system (as in Rust), or through execution in a managed environment.

I think some increase in memory-safe outputs is a part of the 'memory safety story' of C++ regardless of whether this comes from type-level changes or improved constructs for memory safety.

Perhaps, a good way to describe the difference here is to clarify that contracts do not innately make C++ as a language more memory safe, but have plenty to do with the memory safety of C++ applications.


Agreed that it’s not memory safety in the language-guarantee sense. But it lets a library make a spatial-safety guarantee that holds regardless of how it’s compiled.

If I call safe_library::some_low_level_op(some_offset), the library can guarantee a bad offset terminates the program before any out-of-bounds access, and no build flag on my end can drop that check. That’s the same “safe API over unsafe internals, enforced at runtime” pattern Rust’s own standard library relies on for bounds.

What C++ still can’t do is stop me from bypassing that API with a raw pointer, or help at all with lifetimes. But it's useful in practice, not just in theory.


There was a recent discussion about the C++ contracts, and one of the top comments [0][1] in it was that, well, there are already assertion libraries (especially after C++20's concepts) that already do all of that, and better.

[0] https://news.ycombinator.com/item?id=50361334

[1] https://news.ycombinator.com/item?id=50361455


And I really do not like the idea that some third-party library that happened to use decides that I can't turn off a expensive check that would never be triggered in the real world.

Now if the contract was instead, it must be proved at compile time I'd be for that. I don't know if theorem checkers are good enough to pull that off.


How can you know it will never be triggered in the real world? Isn’t that the entire point of this? To catch those bugs and prevent things from going off the rails and continuing on?

If you know it will never happen, what’s the point of the feature?


I don't have a problem with this, library writers are free to design any way they want. If this requires expensive checks, that's a library design choice, and users are free to choose a different library if that's not what they want.

You're aware that third-party library developers have always been able to put all sorts of expensive runtime checks in their code which can't be turned off, right?



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: