{"id":10950,"date":"2025-09-08T09:49:06","date_gmt":"2025-09-08T09:49:06","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10950"},"modified":"2025-09-08T16:04:50","modified_gmt":"2025-09-08T16:04:50","slug":"contracts-evaluation-semantic","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/contracts-evaluation-semantic\/","title":{"rendered":"Contracts: Evaluation Semantic"},"content":{"rendered":"\n<p>After briefly presenting the details of contracts in my last article, \u201c<a href=\"https:\/\/www.modernescpp.com\/index.php\/contracs-a-deep-dive\/\">Contracts: A Deep Dive<\/a>&#8220;, I would like to take a closer look at the very interesting Evaluation Semantic in today&#8217;s article.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"499\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/Time26Contract.png\" alt=\"\" class=\"wp-image-10918\" style=\"width:800px;height:auto\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/Time26Contract.png 682w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/Time26Contract-300x220.png 300w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/figure>\n\n\n\n<p>If a contract assertion occurs, one of the four evaluation semantics is applied: <strong><code>ignore<\/code>, <code>observe<\/code>, <code>enforce<\/code>, <\/strong>or <code><strong>quick-enforce.<\/strong><\/code> The table provides an overview of the four semantics:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"194\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/EvaluationSemantic.png\" alt=\"\" class=\"wp-image-10920\" style=\"width:450px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/EvaluationSemantic.png 860w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/EvaluationSemantic-300x68.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/EvaluationSemantic-768x173.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/EvaluationSemantic-705x159.png 705w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><\/figure>\n\n\n\n<p>Unfortunately, it is not possible to see these evaluation semantics in full use at this time. The following implementations provide an initial impression on Godbolt: <a href=\"https:\/\/godbolt.org\/z\/aqbKTjrTY\">Clang and libc++<\/a> integration, as well as <a href=\"https:\/\/godbolt.org\/z\/WcT1rdnTb\">GCC<\/a>.<\/p>\n\n\n\n<p>First, I would like to discuss the difference between checking semantics and terminating semantics.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Checking Semantic and Terminating Semantic<\/h2>\n\n\n\n<p>While checking semantics evaluates the contract assertion, terminating semantics also terminate the program.<br><\/p>\n\n\n\n<p>Now I finally come to evaluation semantics.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Evaluation Semantic<\/h2>\n\n\n\n<p>A standard-compliant implementation does not have to implement each of the four evaluation semantics: <code>ignore<\/code>, <code>observe<\/code>, <code>enforce<\/code>, and<code> quick-enforce<\/code>. It can also offer its own semantics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>ignore<\/code><\/h3>\n\n\n\n<p>As the name suggests, this semantics ignores the evaluation of the predicate. Nevertheless, the predicate must be syntactically correct.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>observe<\/code><\/h3>\n\n\n\n<p>The <code>observe <\/code>semantics is a so-called checking semantics. Three conditions can, in general, lead to a contract violation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The evaluation of the predicate returns <code>false<\/code>.<\/li>\n\n\n\n<li>The evaluation of the predicate causes an exception.<\/li>\n\n\n\n<li>The evaluation of the predicate occurs at compile time, but the predicate is not a constant expression.<\/li>\n<\/ul>\n\n\n\n<p>If a contract violation occurs at compile time, a diagnostic is produced, and the compilation continues.<\/p>\n\n\n\n<p>If a contract violation occurs at runtime, the contract-violation handler is invoked, referring to an object of type <code>const std::contracts::contract_violation<\/code> containing information about the contract violation. If the contract-violation handler returns normally, program execution continues.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>enforce<\/code><\/h3>\n\n\n\n<p>The enforce semantics will invoke the contract-violation handler at runtime. The program will terminate if the contract-violation handler returns normally. The <code>enforce <\/code>semantics is a so-called terminating semantics. This means that in the event of a contract violation, program execution is terminated. After that, one of the following actions may occur:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Invoke <code>std::terminate.<\/code><\/li>\n\n\n\n<li>Invoke <code>std::abort.<\/code><\/li>\n\n\n\n<li>Terminate execution immediately.<\/li>\n<\/ul>\n\n\n\n<p>Compilation is aborted at compile time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>quick-enforce<\/code><\/h3>\n\n\n\n<p>The<code> quick-enforce<\/code> semantics will not invoke the contract-violation handler at runtime. As a terminating semantics, it will immediately terminate the program. In this case,<code> __builtin_trap() <\/code>is used, for example.<\/p>\n\n\n\n<p>Compilation is aborted at compile time.<\/p>\n\n\n\n<p>Now, of course, I will discuss the contract violation handler.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Contract Violation Handler<\/h2>\n\n\n\n<p>The contract-violation handler has the following signature:<\/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><\/span><span style=\"color: #078; font-weight: bold\">void<\/span><span style=\"color: #BBB\"> <\/span><span style=\"color: #C0F\">handle_contract_violation<\/span>(<span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>contracts<span style=\"color: #555\">::<\/span>contract_violation<span style=\"color: #BBB\"> <\/span>);\n<\/pre><\/div>\n\n\n\n<p>The implementation provides the default contract-violation handler. However, the implementation may also allow this default contract-violation handler to be replaced by a user-defined one.<\/p>\n\n\n\n<p>In his excellent presentation<a href=\"https:\/\/www.youtube.com\/watch?v=u73ZB_vml_c\"> Contracts for C++ <\/a>at ACCU 2025, Timur Doumler presents some exciting examples of user-defined contract violation handlers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Logging<\/li>\n<\/ul>\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><\/span><span style=\"color: #078; font-weight: bold\">void<\/span><span style=\"color: #BBB\"> <\/span><span style=\"color: #C0F\">handle_contract_violation<\/span>(<span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>contracts<span style=\"color: #555\">::<\/span>contract_violation<span style=\"color: #BBB\"> <\/span>violation<span style=\"color: #BBB\"> <\/span>)<span style=\"color: #BBB\"> <\/span>{\n<span style=\"color: #BBB\">    <\/span>LOG(std<span style=\"color: #555\">::<\/span>format(<span style=\"color: #C30\">&quot;Contract violated at: {}<\/span><span style=\"color: #C30; font-weight: bold\">\\n<\/span><span style=\"color: #C30\">&quot;<\/span>,<span style=\"color: #BBB\"> <\/span>violation.location()));<span style=\"color: #BBB\">   <\/span>\n}\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set a breakpoint<\/li>\n<\/ul>\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><\/span><span style=\"color: #078; font-weight: bold\">void<\/span><span style=\"color: #BBB\"> <\/span><span style=\"color: #C0F\">handle_contract_violation<\/span>(<span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>contracts<span style=\"color: #555\">::<\/span>contract_violation<span style=\"color: #BBB\"> <\/span>violation<span style=\"color: #BBB\"> <\/span>)<span style=\"color: #BBB\"> <\/span>{\n<span style=\"color: #BBB\">    <\/span>std<span style=\"color: #555\">::<\/span>breakpoint();\n}\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Wait until a debugger is attached<\/li>\n<\/ul>\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><\/span><span style=\"color: #078; font-weight: bold\">void<\/span><span style=\"color: #BBB\"> <\/span><span style=\"color: #C0F\">handle_contract_violation<\/span>(<span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>contracts<span style=\"color: #555\">::<\/span>contract_violation<span style=\"color: #BBB\"> <\/span>violation<span style=\"color: #BBB\"> <\/span>)<span style=\"color: #BBB\"> <\/span>{\n<span style=\"color: #BBB\">    <\/span><span style=\"color: #069; font-weight: bold\">while<\/span><span style=\"color: #BBB\"> <\/span>(<span style=\"color: #555\">!<\/span>std<span style=\"color: #555\">::<\/span>is_debugger_present())\n<span style=\"color: #BBB\">     <\/span><span style=\"color: #09F; font-style: italic\">\/* spin *\/<\/span>\n\n<span style=\"color: #BBB\">    <\/span>std<span style=\"color: #555\">::<\/span>breakpoint();\n}\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Print a stacktrace<\/li>\n<\/ul>\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><\/span><span style=\"color: #078; font-weight: bold\">void<\/span><span style=\"color: #BBB\"> <\/span><span style=\"color: #C0F\">handle_contract_violation<\/span>(<span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>contracts<span style=\"color: #555\">::<\/span>contract_violation<span style=\"color: #BBB\"> <\/span>violation<span style=\"color: #BBB\"> <\/span>)<span style=\"color: #BBB\"> <\/span>{\n<span style=\"color: #BBB\">    <\/span>std<span style=\"color: #555\">::<\/span>cout<span style=\"color: #BBB\"> <\/span><span style=\"color: #555\">&lt;&lt;<\/span><span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>stacktrace<span style=\"color: #555\">::<\/span>current(<span style=\"color: #F60\">1<\/span>);\n}\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Defer to the default contract-violation handler<\/li>\n<\/ul>\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><\/span><span style=\"color: #078; font-weight: bold\">void<\/span><span style=\"color: #BBB\"> <\/span><span style=\"color: #C0F\">handle_contract_violation<\/span>(<span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>contracts<span style=\"color: #555\">::<\/span>contract_violation<span style=\"color: #BBB\"> <\/span>violation<span style=\"color: #BBB\"> <\/span>)<span style=\"color: #BBB\"> <\/span>{\n<span style=\"color: #BBB\">    <\/span>std<span style=\"color: #555\">::<\/span>cout<span style=\"color: #BBB\"> <\/span><span style=\"color: #555\">&lt;&lt;<\/span><span style=\"color: #BBB\"> <\/span>std<span style=\"color: #555\">::<\/span>stacktrace<span style=\"color: #555\">::<\/span>current(<span style=\"color: #F60\">1<\/span>);\n<span style=\"color: #BBB\">    <\/span>std<span style=\"color: #555\">::<\/span>contracts<span style=\"color: #555\">::<\/span>invoke_default_contract_violation_handler(violation);\n}\n<\/pre><\/div>\n\n\n\n<p>Finally, I would like to introduce the interface of the<code> std::contracts::contract_violation<\/code> object that the contract-violation handler receives.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>std::contracts::contract_violation<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>kind<\/code>: returns the kind of contract assertion violated<\/li>\n\n\n\n<li><code>semantic<\/code>: returns the evaluation semantics when the contract violation occurs<\/li>\n\n\n\n<li><code>is_terminating<\/code>: returns whether the evaluation semantics is terminating<\/li>\n\n\n\n<li><code>detection_mode<\/code>: returns the reason that causes the contract violation<\/li>\n\n\n\n<li><code>evaluation_exception<\/code>: returns an<code> std::exception_ptr <\/code>to the exception thrown from the predicate evaluation<\/li>\n\n\n\n<li><code>comment<\/code>: returns the explanatory string about the contract violation<\/li>\n\n\n\n<li><code>location<\/code>: returns a<code> std::source_location<\/code> indicating the location of the contract violation<\/li>\n<\/ul>\n\n\n\n<p>More information about the<code> std::contracts::contract_violation<\/code> object can be found on the<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/contract\/contract_violation.html\"> cppreference page<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s next?<\/h2>\n\n\n\n<p>In my next article, I will focus on the smaller features in C++26. I&#8217;ll start with the small safety features in the core language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After briefly presenting the details of contracts in my last article, \u201cContracts: A Deep Dive&#8220;, I would like to take a closer look at the very interesting Evaluation Semantic in today&#8217;s article. If a contract assertion occurs, one of the four evaluation semantics is applied: ignore, observe, enforce, or quick-enforce. The table provides an overview [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10918,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[481],"class_list":["post-10950","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\/10950","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=10950"}],"version-history":[{"count":24,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10950\/revisions"}],"predecessor-version":[{"id":11233,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10950\/revisions\/11233"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10918"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}