11

I'm interested in studying the recently accepted contract programming for C++20 for learning and investigation purpose.

As I'm looking around for compiler support, I'm disappointed to not find any. Both gcc and clang are quite clear they do not support this feature within their --std=c++2a mode.

Since the approval is pretty recent, I'm not too surprised that current compilers do not support the exact semantic proposed. What is more surprising to me though is that there is absolutely nothing, not even a compiler-specific extension which would mimic, even in a limited way, the same feature.

I was expecting the C++ committee to only consider features that have proven themselves on the field, typically through compiler or target specific extensions. Approving this feature for C++20 suggests that the feature should be accessible somewhere. But so far, I've been unable to find it.

Is there a way to experiment with C++ contract programming currently ? even using a set of compiler-specific extensions ?

Cyan
  • 13,248
  • 8
  • 43
  • 78
  • 1
    There is a crude implementation of it in the `CppCoreGuidelines` Support Library here: https://github.com/Microsoft/GSL/blob/master/include/gsl/gsl_assert I made similar macros and use then just like `assert`. Or you can just use `assert`. – Galik Dec 11 '18 at 05:39
  • Your link is now pointing to a deleted page. Was that contract programming really accepted? If so, I guess it would have moved or maybe it will be in the next version of C++. – Alexis Wilke Mar 26 '21 at 15:57
  • @Alexis: no, I believe it was ultimately removed, on the ground that making the feature dependent on a compiling mode (analysis, debug, release) was too disruptive, since such a concept is never present anywhere else. – Cyan Mar 27 '21 at 17:26

2 Answers2

5

You might want to try this experimental implementation of contracts in Clang.

r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
3

The July 2019 WG21 meeting in Cologne agreed to remove contracts from the C++20 working draft. See proposal P1823 from Nicolai Josuttis here.

user2023370
  • 10,488
  • 6
  • 50
  • 83
  • Any news about this? I am desperately trying to find a valid piece of code with contracts that can be compiled. There are tons of descriptions with detailed examples but nothing that can be compiled. http://www.hellenico.gr/cpp/w/cpp/language/attributes/contract.html Still lists contracts as C++20 but I cant find them in the overview for C++20 and C++23: https://en.cppreference.com/w/cpp/compiler_support/20 ? – Anonymous Dec 20 '21 at 11:49