I am using constexpr
to get the Fibonacci number
Enumeration is used to calculate the fibonacci in compile time
#include <iostream>
constexpr long fibonacci(long long n)
{
return n < 1 ? -1 :
(n == 1 || n == 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2));
}
enum Fibonacci
{
Ninth = fibonacci(9),
Tenth = fibonacci(10),
Thirtytwo = fibonacci(32)
};
int main()
{
std::cout << Fibonacci(Thirtytwo);
// std::cout << fibonacci(32);
return 0;
}
I am getting the following error on execution:
1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(12): note: while evaluating 'fibonacci(30)' 1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(6): note: while evaluating 'fibonacci(31)' 1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(12): note: while evaluating 'fibonacci(31)' 1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(12): error C2131: expression did not evaluate to a constant 1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(5): note: failure was caused by control reaching the end of a constexpr function 1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(12): note: while evaluating 'fibonacci(32)' 1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(14): error C2057: expected constant expression 1>Done building project "ConsoleApplication4.vcxproj" -- FAILED.
But when I use run time int x=30, y=2; std::cout << fibonacci(x+y);//fibonacci is calculated on run time
I won't say I have a question but I have few confusions like:
constexpr
different?Any example or reference if available will be helpful.
constexpr indicates that the value, or return value, is constant and, where possible, is computed at compile time.
A constexpr function that is eligible to be evaluated at compile-time will only be evaluated at compile-time if the return value is used where a constant expression is required. Otherwise, compile-time evaluation is not guaranteed.
Memory used by the constexpr
function at compile time is implementation dependent, but in general should be comparable to runtime (most compilers will compile and execute the statement).
In theory, you should use compile time evaluated expressions whenever possible. In practice, it's a judgment call (perhaps a good topic for an SE question), as the down sides are increased compile time (and perhaps memory) and lack of debugging.
It appears you are reaching the maximum recursion limit allowed by MSVC in compile time expressions. I can't find any documentation on what this limit is, but it's configurable on other compilers. Your error is the result of the enum
requiring it to be fully evaluated at compile time, where as the cout
call allows it be executed at compile time and/or run time (if you generate assembly, you should see compile time constants generated for the lower number calls, and a recursive function being used for the high number calls).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With