zarzavat 2 hours ago

I have no particular opinion on contracts per se, but I do think that the C++ committee is procrastinating by focusing on niche features like contracts instead of dealing with the urgent issues (namely packaging and memory safety) where C++ is losing to Rust.

The rejection was a moment for them to reevaluate their priorities but it doesn't seem that they've gotten the message.

  • guipsp 2 hours ago

    Contracts are part of the memory safety story in c++

    • edflsafoiewq 2 hours ago

      How?

      • horseloverthin 1 hour ago

        A good way to think about contracts and related features as they relate to memory safety is from the compilers perspective; pretty much any feature that gives the compiler additional information about a users intent for a scope allows the compiler to have to infer less, and thus have more ability to provide 'safety' guarantees.

        The most trivial example of this is a check that a caller has provided non-null inputs ( hence allowing 'safe' unchecked access in most cases)

        • cpgxiii 1 hour ago

          If you're going to rewrite a bunch of C++ to add contracts to assert that your pointer arguments are non-null ... you could, I don't know, just use references instead, the exact construct we have had for decades to express this. If you're cargo-culting the GSG so much you're unwilling to use references appropriately I seriously doubt you are also going to replace whatever existing "pointer argument x is non-null" checks you use with a brand-new contracts mechanism.

          • horseloverthin 53 minutes ago

            There are many common places where this pointer case comes up and using references is simply not an option ( i.e plugin/FFI boundaries, dlsym callbacks, externed C). You will have a very bad time doing any systems programming, for example, if you live by the dogma of shaking your fist at raw pointers and saying use refs instead!

            Perhaps for a more familiar case where contracts allow greater memory safety: constexpr T& operator[](size_t i) const pre(i < size()); constexpr T& front() const pre(!empty());

            or even more commonly: void register_handler(std::unique_ptr<Handler> h) pre(h != nullptr);

            • cpgxiii 29 minutes ago

              Yes, I am distinctly over-simplifying things. There certainly are cases where pointers are appropriate, but I suspect anyone doing this responsibly already has the necessary non-null checks idiomatic to their codebase, and I am very dubious that anyone doing so irresponsibly is going to jump straight to contracts. I only mention the GSG because in the codebases I regularly deal with my experience is that the overwhelming majority of non-nullable pointer arguments appear in cases that adherence to the GSG has created, rather than cases where it is actually necessary for the API.

    • brigade 1 hour ago

      And that’s exactly why they’re so controversial. As adopted by the committee they do not do anything to provide memory safety.

      The pushback from the final rejection got the committee to add the bare minimum that could help C++’s safety story.

      • btown 1 hour ago

        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.

        • zarzavat 48 minutes ago

          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.

          • horseloverthin 24 minutes ago

            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.

        • bluGill 37 minutes ago

          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.

    • leni536 1 hour ago

      Contracts have nothing to do with memory safety.

      • horseloverthin 51 minutes ago

        Contracts have plenty to do with memory safety; the notion of 'hardened implementations' and bound-checked vector access ( 2 enormously popular features) rely on the contracts model.

        • simonask 37 minutes ago

          Assertions were always an option. Unless I'm missing some new development, contracts are just a new way to spell assertions. That's good, but it doesn't move the needle at all in terms of memory safety.

          • horseloverthin 28 minutes ago

            making memory safe patterns easier to implement makes for more memory safety:)

        • aw1621107 28 minutes ago

          Contracts as originally designed by the committee were explicitly not meant to enforce memory safety in that way. You were not supposed to be able to rely on the existence of any particular contract check at runtime. This recent change was something that the committee had rejected earlier, and probably begrudgingly accepted this time around as a compromise to try to ensure the NB vote passes

          The two features you list don't have to depend on contracts, and in fact didn't do so at first. The dependency was only added later, and there were efforts to undo such a change right to the end.

          Furthermore, IIRC hardening only relies on the contracts system to report precondition violations. The actual checks are intentionally not contract assertions and are intentionally not controlled via the same system as other contract assertions (i.e., hardened checks remain even if you use "ignore" as your contract evaluation semantic).

  • Aerocatia 34 minutes ago

    I think the Common Package Specification goes a long way to fix packaging with C++ after trying it

  • GuB-42 28 minutes ago

    Why should C++ "win" over Rust?

    C++ already has the tools needed for memory safety, but it will never like Rust without serious compromises. Remember, C++ is C with extra features (except for a few minor details), C is not memory safe and will never be, so, for C++ to become memory safe, it has to depart from C, which to me is completely missing the point of C++. To me, the point of C++ is: "I want to write C, but I also want feature X", where is can be templates, classes, smart pointers, namespaces, etc... I don't have to use them all at once. For instance, if the only thing that interests me are templates, I just write my C code with raw pointers and all that, have my templates and compile it with a C++ compiler.

    As for packages, there are already packages managers for C++, such as Conan. Unlike Rust, C++ is just a language, Rust is a trademarked brand, with a foundation that manages a whole ecosystem with the language itself but also a packet manager, build system, repository, community, etc... Different scope. For standardizing packaging, one would need a different committee, which may or may not align with the C++ revisions.

    As for the "niche features", it is again the idea that C++ is C with extra features. Some people need contracts, give them contracts, others can simply ignore them. It is just another tool in the toolbox. Unlike Rust-like memory safety, it won't affect those who don't need it.

    If you want Rust, use Rust, don't make C++ be Rust. Or maybe try some of the alternatives like Carbon.

    • aw1621107 21 minutes ago

      > For standardizing packaging, one would need a different committee, which may or may not align with the C++ revisions.

      I don't think you need a separate committee. There were efforts to create a standardized packaging system through the committee, but the author decided to pursue a different path outside of the committee after it was clear that the committee's priorities were elsewhere.

    • woodruffw 6 minutes ago

      It's worth noting that C and C++ have already diverged significantly. The two have different undefined behavior, different aliasing and punning semantics, etc.

      (The two most famous examples of this are type punning, which is well-defined in C but not in C++, and infinite loops without side effects, which were UB in C++ until this year because of C++'s stricter forward progress guarantee.)

  • BearOso 24 minutes ago

    They need a big ABI or possibly even API break to clean up. It's common enough to specify standards on the compiler command line now, and i.e. Microsoft's runtimes have handled multi-versioning for a long time.

grg0 2 hours ago

> pre<std::contracts::controls::enforced>(x > 0)

Ho-lee, guys. We just needed pre(x > 0)

  • andrewjf 2 hours ago

    Yeah but why wouldn't "x" be a type that could only represent positive integers?

    • kevin_thibedeau 2 hours ago

      They really should just copy Ada rather than this whizbangery.

      • gumby 1 hour ago

        It would even be more cumbersome to add that and still allow it to interoperate with legacy code. The C and Rust types, or even the more powerful C++ types today would all still have to work in mixed code.

        A shame, but that's life.

    • grg0 2 hours ago

      I would like Ada-style integer types too, but I don't strictly know how those are implemented. Are you suggesting invariants should also be given at the type level?

    • Joker_vD 1 hour ago

      Have you seen the wave of recent Whatever2Shell CVEs? Several of them happened precisely because "a type that could only represent positive integers" was used for the sizes.

      • grg0 1 hour ago

        I don't think that's what he's talking about. The problem with those CVEs is an unsigned type with default wrap semantics. But it need not be like that. Look at how Ada allows user-defined integers.

npalli 2 hours ago

LOL. The C++26 gloom and doom last month was something else. C++ was finished (not just C++26, mind you), and everyone should pack their bags and move to Rust.

Meanwhile, back in reality, the committee did its job: it addressed the NB comments, kept contracts, and C++26 is back on track. Imagine that.

  • gumby 1 hour ago

    Rust's type system is less powerful and less expressive than C++ (arguably in exchange for improvements in certain cases of memory safety), and adding some of that in will bringe it closer to C++ which rust people presumably wouldn't be in favor of.

  • qweqwe14 1 hour ago

    It's always fun to see C++ fanatics cope. Surely <next C++ standard> will finally be better than Rust.

    And you get to actually use it 5 years later too!

jcranmer 50 minutes ago

I'll say this about WG21: the C++ standardization process is definitely deeply broken. And yet one of the most frustrating things is... for all of the deeply problematic stuff coming out of C++, contracts is one of the features that actually doesn't suffer from WG21's dysfunction!

The problem with contracts is that everyone has a slightly different notion of what contracts is supposed to solve, and it took years (really, over a decade) to come a consensus that almost everybody was happy with. Except for a small, vocal minority who are so upset over contracts that they're willing to spike the entire process to kill it. While this trip report is putting a positive spin on it, if you look at the dissenting vote counts in the plenary votes, they're not really going down, and I'm dubious that any of the dissenters were actually mollified by the latest changes. My suspicion is that C++26 NB balloting round 2 will come back the same way--failure, just with a couple thousand extra man-hours wasted on debating it.

As for why WG21 is so broken, well, I hope you'll forgive me a bit of rant:

It was one of the papers I was listening to during this meeting, about a new proposed standard library feature. And as the author is droning on about how good their implementation is, thanks to their thousands of lines of inline assembly, I just wanted to stop and shout at them "I don't give a flying fuck about your implementation." When you propose adding something to the C++ standard library, it's not your implementation that's going to be added. No, each of the standard libraries is going to reimplement it themselves because they have to--and given that all of their maintainers seem to be running on the ragged edge of burnout because the new library additions just keep coming, it's probably going to be the minimum work necessary to get something working.

And yet every library paper author comes in with their wistful stories of how good their implementation is, how nice it would be if everyone could take advantage of their work, and of course, since it's in the standard library, you get the best possible implementation, as if <linalg> is going to be implemented by a BLAS expert who's been studying it for 30 years and not some guy who hasn't touched numerics in 30 years but needs to check off a C++26 feature. Or hey, maybe even the compiler will get super special awesome sauce for optimization for the feature, because obviously that kind of integration is just free, right?

And yet... you just can't actually bring any of this stuff up to the committee. WG21 is so compartmentalized that trying to bring it up in any room is going to just result in "that's not relevant for this vote, bring it up in <insert study group here>." The only thing you can do is be a curmudgeon who votes against everything because it's really poorly motivated, and yet you'll be outvoted by everybody else who is bringing their own library features into the standard, which all of the same flaws and thus their authors are equally blind to the issues, so they'll vote in favor because it sounds like a really cool thing to add.