Urgent.News

What's breaking now, across thousands of outlets.

Tech

C++26 Contracts: Introduction to Preconditions and Postconditions

C++26 introduces a new feature called contracts that allows developers to specify preconditions, postconditions, and invariants within their code. Preconditions are conditions that must be true when a function is called, while postconditions describe the state of the program after the function has completed successfully. This article explores how to use contracts in C++26 and how they differ from traditional assertions like assert().

Contracts are declared using new syntax such as pre(index < values.size()) before a function parameter list. This introduces a precondition that must hold when the function is entered. Contracts can also be added to function declarations, making it easier to document APIs and enforce requirements without running the code.

One key difference between contracts and assert() is that contracts are associated with the function's declaration rather than its implementation. This means that the requirements are visible without having to look inside the function's body. Additionally, contracts are not part of the function's type, so they don't affect overload resolution.

When a contract is violated, the program can take different actions based on the specified policy. The default policy is to terminate the program with a diagnostic message, but developers can also choose to observe the violation without terminating or even enforce the violation, which also terminates the program.

In addition to preconditions and postconditions, contracts can also include invariants, which are conditions that must always hold true within a function. These are checked at various points inside the function, such as after each loop iteration or after processing a specific block of data.

When migrating from traditional assertions to contracts, developers should be aware of some important considerations. One issue is mutation inside predicates, where contract predicates use special constification rules. This means that certain operations like incrementing a variable inside a contract predicate may be rejected by the compiler.

Overall, contracts in C++26 provide a powerful way to specify and enforce requirements within your code, making it easier to document APIs, catch potential errors, and ensure correct program behavior. As the feature becomes more widely supported across compilers, it is becoming an increasingly valuable tool for modern C++ development.

Written by urgent.news from Hacker News's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at cppstories.com →

More in Tech

Basecamp's Home Page Gave Me Two Valid Phone Numbers. Both Were Calendar Day Numbers

I run a visa agency in Bali. The other half of my week goes into Apify Actors, and one of them does something that sounds trivial until you try it: you give it a list of websites, it gives you back…

  • Two valid phone numbers extracted from Basecamp's home page were actually calendar day numbers.
  • Numbers +1 301 516 1718 and +1 301 213 1415 formed sequences of ten-digit day numbers.
  • Basecamp's actual contact information was identified through rank and schema.org markup.

PokeSurvivor

I’m building PokeSurvivor Online, a free browser survival game using Astro and Canvas. Choose Bulbasaur, Charmander, or Squirtle, collect Stardust, and spend it in shops between stages to shape your…

More from Thursday 15 October →