diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2F_static_assert.html | |
new repository
Diffstat (limited to 'devdocs/c/language%2F_static_assert.html')
| -rw-r--r-- | devdocs/c/language%2F_static_assert.html | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/devdocs/c/language%2F_static_assert.html b/devdocs/c/language%2F_static_assert.html new file mode 100644 index 00000000..9ceeb593 --- /dev/null +++ b/devdocs/c/language%2F_static_assert.html @@ -0,0 +1,58 @@ + <h1 id="firstHeading" class="firstHeading">Static assertion <span class="t-mark-rev t-since-c11">(since C11)</span> +</h1> <h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>_Static_assert</code> <code>(</code> <span class="t-spar">expression</span> <code>,</code> <span class="t-spar">message</span> <code>)</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span><span class="t-mark-rev t-deprecated-c23">(deprecated in C23)</span> </td> +</tr> <tr class="t-sdsc"> <td> <code>static_assert</code> <code>(</code> <span class="t-spar">expression</span> <code>,</code> <span class="t-spar">message</span> <code>)</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> <tr class="t-sdsc"> <td> <code>_Static_assert</code> <code>(</code> <span class="t-spar">expression</span> <code>)</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span><span class="t-mark-rev t-deprecated-c23">(deprecated in C23)</span> </td> +</tr> <tr class="t-sdsc"> <td> <code>static_assert</code> <code>(</code> <span class="t-spar">expression</span> <code>)</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> +</table> <table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">expression</span> </td> <td> - </td> <td> any <a href="constant_expression" title="c/language/constant expression">integer constant expression</a> </td> +</tr> <tr class="t-par"> <td> <span class="t-spar">message</span> </td> <td> - </td> <td> any <a href="string_literal" title="c/language/string literal">string literal</a> </td> +</tr> +</table> <table class="t-rev-begin"> <tr class="t-rev t-until-c23"> +<td> <p>This keyword is also available as convenience macro <a href="../error/static_assert" title="c/error/static assert"><code>static_assert</code></a>, available in the header <a href="../error" title="c/error"><code><assert.h></code></a>.</p> +</td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td> +</tr> <tr class="t-rev t-since-c23"> +<td> <p>Both of <code>static_assert</code> and <code>_Static_assert</code> have the same effects. <code>_Static_assert</code> is a deprecated spelling that is kept for compatibility.</p> +<p>An implementation may also define <code>static_assert</code> and/or <code>_Static_assert</code> as predefined macros, and <code>static_assert</code> is no longer provided by <a href="../error" title="c/error"><code><assert.h></code></a>.</p> +</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <h3 id="Explanation"> Explanation</h3> <p>The constant expression is evaluated at compile time and compared to zero. If it compares equal to zero, a compile-time error occurs and the compiler <span class="t-rev-inl t-until-c23"><span>must display <span class="t-spar">message</span> as part of the error message (except that characters not in <a href="charset" title="c/language/charset">basic character set</a> are not required to be displayed)</span><span><span class="t-mark-rev t-until-c23">(until C23)</span></span></span><span class="t-rev-inl t-since-c23"><span>should display <span class="t-spar">message</span> (if provided) as part of the error message</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span>.</p> +<p>Otherwise, if <span class="t-spar">expression</span> does not equal zero, nothing happens; no code is emitted.</p> +<h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/_static_assert" title="c/keyword/ Static assert"><code>_Static_assert</code></a>, <a href="../keyword/static_assert" title="c/keyword/static assert"><code>static_assert</code></a></p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <assert.h> // no longer needed since C23 + +int main(void) +{ + // Test if math works, C23: + static_assert((2 + 2) % 3 == 1, "Whoa dude, you knew!"); + // Pre-C23 alternative: + _Static_assert(2 + 2 * 2 == 6, "Lucky guess!?"); + + // This will produce an error at compile time. + // static_assert(sizeof(int) < sizeof(char), "Unmet condition!"); + + constexpr int _42 = 2 * 3 * 2 * 3 + 2 * 3; + static_assert(_42 == 42); // the message string can be omitted. + + // const int _13 = 13; + // Compile time error - not an integer constant expression: + // static_assert(_13 == 13); +}</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul><li> 6.7.11 Static assertions (p: TBD) </li></ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 6.7.10 Static assertions (p: 105) </li> +<li> 7.2 Diagnostics <assert.h> (p: 135) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 6.7.10 Static assertions (p: 145) </li> +<li> 7.2 Diagnostics <assert.h> (p: 186-187) </li> +</ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../error/assert" title="c/error/assert"> <span class="t-lines"><span>assert</span></span></a></div> </td> <td> aborts the program if the user-specified condition is not <code>true</code>. May be disabled for release builds <br> <span class="t-mark">(function macro)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/static_assert" title="cpp/language/static assert">C++ documentation</a></span> for <span class=""><span><code>static_assert</code> declaration</span></span> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/language/_Static_assert" class="_attribution-link">https://en.cppreference.com/w/c/language/_Static_assert</a> + </p> +</div> |
