{"id":4979,"date":"2016-10-14T05:44:20","date_gmt":"2016-10-14T05:44:20","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/statically-checked\/"},"modified":"2023-06-26T12:39:36","modified_gmt":"2023-06-26T12:39:36","slug":"statically-checked","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/statically-checked\/","title":{"rendered":"Statically Checked"},"content":{"rendered":"<p><span style=\"font-family: courier new,courier;\">static_assert<\/span> is the tool in modern C++ to make your code safe.<\/p>\n<p><!--more--><\/p>\n<h2>static_assert<\/h2>\n<p>The usage of <span style=\"font-family: courier new,courier;\">static_asser<\/span>t is relatively easy. <span style=\"font-family: courier new,courier;\">static_assert<\/span> requires an expression and a string. The expression must be a predicate that can be evaluated at compile time. Predicate means the expression returns <span style=\"font-family: courier new,courier;\">true<\/span> or <span style=\"font-family: courier new,courier;\">false.<\/span> If the expression evaluates to <span style=\"font-family: courier new,courier;\">false<\/span> you will get at compile time an error message with the string as a message. Of course, you get no executable.<\/p>\n<\/p>\n<p>There are a few points you have to keep in your mind.<\/p>\n<ul>\n<li>The <span style=\"font-family: courier new,courier;\">static_assert<\/span>&nbsp;expression will be evaluated at compile-time, and you have no runtime overhead.<\/li>\n<li>Expressions that can be evaluated at compile time are called constant expressions. I have much to tell about the new keyword <span style=\"font-family: courier new,courier;\">constexpr;<\/span> but in a later post. <span style=\"font-family: courier new,courier;\"><\/span><\/li>\n<li>You can use<span style=\"font-family: courier new,courier;\"> static_assert<\/span> expressions in all parts of your program. Therefore, Putting general requirements on your source code in a separate header is a good idea. The result is, the static_assert expression will be automatically verified at compile time if you include the header.&nbsp;<\/li>\n<\/ul>\n<p>Here is the code example to <span style=\"font-family: courier new,courier;\">static_assert.<\/span><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ static_assert.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\r\n\r\nstatic_assert(<span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #2b91af;\">void<\/span>*) == 4 ,<span style=\"color: #a31515;\">\"32-bit adressing is required!\"<\/span>);\r\nstatic_assert(<span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #2b91af;\">void<\/span>*) &gt;= 8 ,<span style=\"color: #a31515;\">\"64-bit adressing is required!\"<\/span>);\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt; <span style=\"color: #0000ff;\">typename<\/span> T, <span style=\"color: #2b91af;\">int<\/span> Line, <span style=\"color: #2b91af;\">int<\/span> Column &gt;\r\n<span style=\"color: #0000ff;\">struct<\/span> Matrix{\r\n   static_assert(Line &gt;= 0, <span style=\"color: #a31515;\">\"Line number must be positive.\"<\/span>);\r\n   static_assert(Column &gt;= 0, <span style=\"color: #a31515;\">\"Column number must be positive.\"<\/span>);\r\n   static_assert( Line + Column &gt; 0, <span style=\"color: #a31515;\">\"Line and Column must be greater than 0.\"<\/span>);\r\n};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main() {\r\n  \r\n  static_assert(<span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #2b91af;\">int<\/span>) == <span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">int<\/span>),<span style=\"color: #a31515;\">\"int and long int must be of the same length.\"<\/span>);\r\n  \r\n  Matrix&lt;<span style=\"color: #2b91af;\">int<\/span>,10,5&gt; intArray;\r\n  Matrix&lt;std::string,3,4&gt; strArray;\r\n  Matrix&lt;<span style=\"color: #2b91af;\">double<\/span>,0,1&gt; doubleArray;\r\n  Matrix&lt;<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">long<\/span>,1,0&gt; longArray;\r\n  \r\n  Matrix&lt;<span style=\"color: #2b91af;\">char<\/span>,0,0&gt; charArray;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The program uses <span style=\"font-family: courier new,courier;\">static_assert<\/span> in the global scope (lines 5 and 6); it uses it in the class scope (lines 10 &#8211; line 12); uses it in the local scope (line 17). One of the two assertions in lines 5 and 6 must inevitably fall. The assertions in the class definition guarantee, on the one hand, that the number of columns and lines must be greater than 0, and it guarantees, on the other hand, that the sum has to be positive. That&#8217;s why the template instantiation in line 14 is invalid. On my computer <span style=\"font-family: courier new,courier;\">int<\/span> is more minor than <span style=\"font-family: courier new,courier;\">long int<\/span>. The C++ standard guarantees the reverse relation.<\/p>\n<p>Let&#8217;s have a look at the broken run of my compiler.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4978\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/static_assert.png\" alt=\"static assert\" width=\"800\" height=\"284\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/static_assert.png 902w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/static_assert-300x106.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/static_assert-768x272.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The power of <span style=\"font-family: courier new,courier;\">static_assert<\/span> is due to what can be evaluated at compile time. Glad we have the new type-traits library in C++11. The powerful type-traits library empowers you to check, compare and change types at compile time. But this is the story of the <a href=\"https:\/\/www.modernescpp.com\/index.php\/more-and-more-save\">next post.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>static_assert is the tool in modern C++ to make your code safe.<\/p>\n","protected":false},"author":21,"featured_media":4978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[518],"class_list":["post-4979","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-static_assert"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4979","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=4979"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4979\/revisions"}],"predecessor-version":[{"id":6937,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4979\/revisions\/6937"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4978"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}