{"id":10182,"date":"2024-10-21T09:54:45","date_gmt":"2024-10-21T09:54:45","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10182"},"modified":"2025-07-04T14:46:31","modified_gmt":"2025-07-04T14:46:31","slug":"contracts-in-c26","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/contracts-in-c26\/","title":{"rendered":"Contracts in C++26"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Contracts allow you to specify preconditions, postconditions, and invariants for functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"505\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contracts-1030x505.png\" alt=\"\" class=\"wp-image-10185\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contracts-1030x505.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contracts-300x147.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contracts-768x377.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contracts-705x346.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contracts.png 1054w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Contracts should already be part of C++20 but were removed in the standard meeting in Cologne. Here is what Herb Sutter said about contracts on <a href=\"https:\/\/herbsutter.com\/2018\/07\/02\/trip-report-summer-iso-c-standards-meeting-rapperswil\/\">Sutter\u2019s Mill<\/a>: \u201c<em>contracts is the most impactful feature of C++20 so far, and arguably the most impactful feature we have added to C++ since C++11<\/em>.\u201d. With C++26, we probably get them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post is based on the proposal <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2023\/p2961r2.pdf\">P2961R2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Contract?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A contract specifies interfaces for software components in a precise and checkable way. These software components are functions and methods that must fulfill preconditions, postconditions, and invariants. Here are the definitions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>precondition<\/strong>: a predicate that is supposed to hold upon entry in a function.<\/li>\n\n\n\n<li>A <strong>postcondition<\/strong>: a predicate that is supposed to hold upon exit from the function.<\/li>\n\n\n\n<li>An <strong>assertion<\/strong>: a predicate that is supposed to hold at its point in the computation. <\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The precondition and the postcondition are placed outside the function definition, but the invariant is placed inside the function definition. A predicate is an expression that returns a boolean.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before I show you the first example, let me write about the contract design goals.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Design Goals <\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>The syntax should fit naturally into existing C++. The intent should be intuitively understandable by users unfamiliar with contract checks without creating any confusion.<\/em><\/li>\n\n\n\n<li><em>A contract check should not resemble an attribute, a lambda, or any other pre-existing C++ construct. It should sit in its own, instantly recognisable design space.<\/em><\/li>\n\n\n\n<li><em>The syntax should feel elegant and lightweight. It should not use more tokens and character than necessary.<\/em><\/li>\n\n\n\n<li><em>To aid readability, the syntax should visually separate the different syntactic parts of a contract check. It should be possible to distinguish at a glance the contract kind, the predicate, the name for the return value &#8230;<\/em> (Proposal <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2023\/p2961r2.pdf\">P2961R2<\/a>)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><br>Now comes the first example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">First example<\/h2>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">f<\/span>(<span style=\"color: #007788; font-weight: bold\">int<\/span> i)\n    pre (i <span style=\"color: #555555\">&gt;=<\/span> <span style=\"color: #FF6600\">0<\/span>)\n    post (r<span style=\"color: #555555\">:<\/span> r <span style=\"color: #555555\">&gt;<\/span> <span style=\"color: #FF6600\">0<\/span>)\n{\n    contract_assert (i <span style=\"color: #555555\">&gt;=<\/span> <span style=\"color: #FF6600\">0<\/span>);\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> i<span style=\"color: #555555\">+<\/span><span style=\"color: #FF6600\">1<\/span>;\n}\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><code>pre<\/code> and <code>post<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>adds a precondition (postcondition). A function can have an arbitrary number of preconditions.(postconditions). They can be intermingled arbitrarily.<\/li>\n\n\n\n<li>are a contextual keyword. A contextual keyword is a keyword in specific contexts but an identifier outside that context.<\/li>\n\n\n\n<li> are positioned at the end of the function declaration.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><code>post<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>can have a return value. An identifier must be placed before the predicate, followed by a colon.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><code>contract_assert<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li> is a keyword. Otherwise, it could not be distinguished from a function call.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You may wonder why the assertion has such a long keyword.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>assert <\/code>Issue<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The ideal keyword for the assertion would be <code>assert <\/code>but not <code>contract_assert<\/code>. <code>assert <\/code>is used in most programming languages to express contract-like assertions. But C++ has a legacy issue.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #009999\">#include &lt;cassert&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">f<\/span>() {\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> i <span style=\"color: #555555\">=<\/span> get_i();\n    assert(i <span style=\"color: #555555\">&gt;=<\/span> <span style=\"color: #FF6600\">0<\/span>); <span style=\"color: #0099FF; font-style: italic\">\/\/ identical syntax for contract assert and macro assert!<\/span>\n    use_i(i);\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>assert<\/code> is already a macro from the header <code>&lt;cassert&gt;<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Break Of Contract<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"> The break of the contract causes a runtime error.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ contract.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">f<\/span>(<span style=\"color: #007788; font-weight: bold\">int<\/span> i)\n    pre (i <span style=\"color: #555555\">&gt;=<\/span> <span style=\"color: #FF6600\">0<\/span>)\n    post (r<span style=\"color: #555555\">:<\/span> r <span style=\"color: #555555\">&gt;<\/span> <span style=\"color: #FF6600\">0<\/span>)\n{\n    contract_assert (i <span style=\"color: #555555\">&gt;=<\/span> <span style=\"color: #FF6600\">0<\/span>);\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> i<span style=\"color: #555555\">+<\/span><span style=\"color: #FF6600\">1<\/span>;\n}\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;    \n    \n    f(<span style=\"color: #555555\">-<\/span><span style=\"color: #FF6600\">1<\/span>);\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    \n}\n<\/pre><\/div>\n\n\n\n<div style=\"height:29px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"691\" height=\"101\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contract.png\" alt=\"\" class=\"wp-image-10265\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contract.png 691w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/10\/Contract-300x44.png 300w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">My next post will continue with the minor C++26 core language features.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contracts allow you to specify preconditions, postconditions, and invariants for functions. Contracts should already be part of C++20 but were removed in the standard meeting in Cologne. Here is what Herb Sutter said about contracts on Sutter\u2019s Mill: \u201ccontracts is the most impactful feature of C++20 so far, and arguably the most impactful feature we [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10185,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[481],"class_list":["post-10182","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog","tag-contracts"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=10182"}],"version-history":[{"count":23,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10182\/revisions"}],"predecessor-version":[{"id":10855,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10182\/revisions\/10855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10185"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}