C++ Logo


Advanced search

[wg14/wg21 liaison] P2961R1 syntax for Contracts: viable for C?

From: Timur Doumler <cpp_at_[hidden]>
Date: Thu, 5 Oct 2023 14:58:51 +0300
Hello SG22,

In SG21 (Contracts), we have been discussing the syntax for preconditions, postconditions, and assertions which will be part of the new Contracts facility we're designing for C++26. We currently have two syntax proposals on the table – we have not yet decided which one we prefer.

One is the attribute-like syntax which you might have already seen (see P2935R3 <https://wg21.link/p2935r3>). With this syntax, a function declaration with a precondition and a postcondition looks like this:

int f(int i)
  [[ pre: i >= 0 ]]
  [[ post r: r > i ]];

The other is a newer proposal that seeks to eliminate the various issues with attribute-like syntax we discovered (see P2961R1 <https://wg21.link/p2961r1>, which I am co-authoring together with Jens Maurer, in Cc). The P2961R1 syntax looks like this:

void f(int i)
  pre (i >= 0)
  post (r: r > i);

At the last SG21 telecon, the question has been raised whether the P2961R1 syntax would be compatible with C, in case C wishes to standardise a Contracts facility consistent with the C++ one. As the main paper author of P2961R1, I was directed to ask WG14 about their opinion and report back to SG21.

However, I have been informed by Ville (in Cc) that WG14 is currently doing a ballot, that the WG14 Convener is adamant about having as little as possible technical discussion during a ballot, and that discussing a future proposal on the WG14 reflector would therefore be inappropriate at this time.

So, if you don't mind, I'm going to try my luck with SG22 instead. Do any of you folks have any desire to standardise Contracts for C, to have a syntax for this that looks like the C++ one, and do you have any technical concerns about the syntax proposed in P2961R1 <https://wg21.link/p2961r1>?

Any and all feedback would be greatly appreciated.


Received on 2023-10-05 11:58:54