How to calculate some mathematical expression i.e. finding factorial of n; during compiletime in c++ ?
How to calculate some mathematical expression i.e. finding factorial of n; during compiletime in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is a wiki article about template meta-programing in C++.
Here is another wiki article about compile-time function execution.
Here is a SO question regarding the factorial.
Let’s take the wiki xample of computing factorial in compile-time.
Since all of the arguments needed are known in the compile time (they are explicitly mentioned in
Factorial<4>, for example), the compiler is able to generate all the needed code. After that, thevalueof theFactorial<4>struct will be 24 which can be later used as if you’ve hardcoded it yourself.