Compile-time function execution: Difference between revisions

Content deleted Content added
Added synonym "generalized constant expressions" to lede
Example: Expanded C++ constexpr example
Line 25:
</source>
 
Using compile-time function evaluation, code used to compute the factorial would be exactlysimilar the same asto what one would write for run-time evaluation e.g. using C++11 constexpr.
 
<source lang="CPP">
#include <stdio.h>
constexpr int factorial(int n) {
return n ? (n * factorial(n - 1)) : 1;
}
constexpr int f10 = factorial(10);
int main() {
printf("%d\n", f10);
return 0;
}
</source>
 
In [[C++11]], this technique is known as [[C++11#constexpr – Generalized constant expressions|generalized constant expressions]](<code>constexpr</code>).<ref>{{cite web|url=http://www.stroustrup.com/sac10-constexpr.pdf|author=Gabriel Dos Reis and Bjarne Stroustrup | title=General Constant Expressions for System Programming Languages. SAC-2010. The 25th ACM Symposium On Applied Computing. | date=March 2010}}</ref> [[C++14]] [[C++14#Relaxed constexpr restrictions|relaxes the constraints]] on constexpr – allowing local declarations and use of conditionals and loops (the general restriction that all data required for the execution be available at compile-time remains).