summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fconstexpr.html
blob: 5cffe330a0fba02647c2b1afea3cf8ad9f1f6fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    <h1 id="firstHeading" class="firstHeading">constexpr specifier <span class="t-mark-rev t-since-c23">(since C23)</span>
</h1>            <p>A scalar object declared with the <code>constexpr</code> storage-class specifier is a <a href="constant_expression" title="c/language/constant expression">constant</a>. It must be fully and explicitly initialized according to the static initialization rules. It still has linkage appropriate to its declaration and it exist at runtime to have its address taken; it simply cannot be modified at runtime in any way, i.e. the compiler can use its knowledge of the object’s fixed value in any other <a href="constant_expression" title="c/language/constant expression">constant expression</a>.</p>
<p>Additionally, the constant expression that is used for the initializer of such a constant is checked at compile time.</p>
<p>An initializer of floating-point type must be evaluated with the translation-time floating-point environment.</p>
<p>There are some restrictions on the type of an object that can be declared with <code>constexpr</code>. Namely, the following constructs are not allowed to be <code>constexpr</code>:</p>
<ul>
<li> <a href="pointer" title="c/language/pointer">Pointers</a> (except that null pointers can be <code>constexpr</code>), </li>
<li> Variably modified types, </li>
<li> <a href="atomic" title="c/language/atomic">Atomic types</a>, </li>
<li> <a href="volatile" title="c/language/volatile"><code>volatile</code></a> types, </li>
<li> <a href="restrict" title="c/language/restrict"><code>restrict</code></a> pointers. </li>
</ul>  <h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/constexpr" title="c/keyword/constexpr"><code>constexpr</code></a></p>
<h3 id="Notes"> Notes</h3> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;fenv.h&gt;
#include &lt;stdio.h&gt;
 
int main(void)
{
    constexpr float f = 23.0f;
    constexpr float g = 33.0f;
    fesetround(FE_TOWARDZERO);
    constexpr float h = f / g; // is not affected by fesetround() above
    printf("%f\n", h);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">0.696969</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> TBD TBD (p: TBD) </li></ul>
</ul>   <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/constexpr" title="cpp/language/constexpr">C++ documentation</a></span> for <span class=""><span><code>constexpr</code> type specifier</span></span> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/language/constexpr" class="_attribution-link">https://en.cppreference.com/w/c/language/constexpr</a>
  </p>
</div>