Why no output for constexpr and normal fibonaci | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why no output for constexpr and normal fibonaci

Hi I thought constexpr is compile time evaluation and fibonaci of 43 with constexpr will be quick compared to non constexpr version Whats missing here as I am not getting any output? https://code.sololearn.com/cFR00PH9KfCR/?ref=app

11th Dec 2022, 11:45 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 3
constexpr doesn't necessarily mean that the function *will* be evaluated at compile-time. It just specifies that it *can* be. You need to make a constexpr variable and assign the result of the function to it, which will force the function to be evaluated at compile time. https://code.sololearn.com/cvfWYc0CmP84/?ref=app The error now, as is said in the error message, is because fibonacci<43> exceeds the operation count limit. The recursive implementation of fibonacci results in a huge number of calls to the fibonacci function (like, a very huge number) since there is no memoization of the function calls AFAIK in GCC. You can use the iterative approach instead for the calculating the results https://code.sololearn.com/cAV7dr59LYlm/?ref=app It works, and as expected, the fibonacciOld() call fails to evaluate in the time limit, while fibonacci1<43>() works just fine
11th Dec 2022, 12:49 PM
XXX
XXX - avatar
0
Thanks XXX
11th Dec 2022, 2:17 PM
Ketan Lalcheta
Ketan Lalcheta - avatar