I read about contracts in Thoughts about C++17 by B. Stroustrup and assisted a small presentation talking about them but I am not sure I have understood them really.
So I have some interrogations and if it is possible to illustrate them with some examples :
Are contracts just a better replacement of the classic
assert()
, and should they be used together? What contracts really are put in simple terms for a software dev?Would contracts have an impact on how we handle exceptions? If yes, how should we use exceptions and contracts?
Would using contracts imply an overhead at execution time? Will we be allowed to deactivate them on release code?
From the proposal N4415:
A pre-condition contract of the indexing operator of a Vector class could be written:
T& operator[](size_t i) [[expects: i < size()]];
Similarly, a post-condition contract on a constructor of an ArrayView class could be expressed as:
ArrayView(const vector<T>& v) [[ensures: data() == v.data()]];
Thanks to @Keith Thompson comment:
Contracts didn't make it into C++20. A new study group, SG21, has been created.