<story><title>C++17 constexpr everything, or as much as the compiler can</title><url>https://solarianprogrammer.com/2017/12/27/cpp-17-constexpr-everything-as-much-as-the-compiler-can/</url></story><parent_chain></parent_chain><comment><author>WalterBright</author><text>D doesn&amp;#x27;t need constexpr. It&amp;#x27;s much simpler - any function whose value is needed at compile time is evaluated at compile time. For example:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    int square(int x) { return x * x; }
    const y = square(3); &amp;#x2F;&amp;#x2F; evaluated at compile time
    int bar() {
        int[square(2)] array; &amp;#x2F;&amp;#x2F; evaluated at compile time
        return square(3); &amp;#x2F;&amp;#x2F; evaluated at run time
    }
&lt;/code&gt;&lt;/pre&gt;
If a value is needed at compile time, and the function cannot be evaluated at compiler time (i.e. it relies on things like global variables not known at compile time) then a compilation error is issued.&lt;p&gt;There isn&amp;#x27;t any ambiguity about whether it is evaluated at runtime or compile time - there is no &amp;quot;fall back&amp;quot; to run time if it can&amp;#x27;t be done at compile time.</text></comment>
<story><title>C++17 constexpr everything, or as much as the compiler can</title><url>https://solarianprogrammer.com/2017/12/27/cpp-17-constexpr-everything-as-much-as-the-compiler-can/</url></story><parent_chain></parent_chain><comment><author>andrepd</author><text>&amp;gt;In conclusion, at the time of this writing, you need to inspect the generated code of your compiler, if you want to be sure that something is really calculated at compile time.&lt;p&gt;I don&amp;#x27;t get this. Why? Doesn&amp;#x27;t making a variable constexpr ensure it is a compile time value, or it will fail to compile?</text></comment>