constexpr question, why do these two different programs run in such a different amount of time with g++?
c++, c++11, constexpr, g++
Solution
`joe` is an Integral Constant Expression; it must be usable in array bounds. For that reason, a reasonable compiler will evaluate it at compile time.
In your second program, even though the compiler could calculate it at compile time, there's no reason why it must.
Problem
I'm using gcc 4.6.1 and am getting some interesting behavior involving calling a `constexpr` function. This program runs just fine and straight away prints out `12200160415121876738`. ``` #include <iostream> extern const unsigned long joe; constexpr unsigned long fib(unsigned long int x) { return (x <= 1) ? 1 : (fib(x - 1) + fib(x - 2)); } const unsigned long joe = fib(92); int main() { ::std::cout << "Here I am!\n"; ::std::cout << joe << '\n'; return 0; } ``` This program takes forever to run and I've never had the patience to wait for it to print out a value: ``` #include <iostream> constexpr unsigned long fib(unsigned long int x) { return (x <= 1) ? 1 : (fib(x - 1) + fib(x - 2)); } int main() { ::std::cout << "Here I am!\n"; ::std::cout << fib(92) << '\n'; return 0; } ``` Why is there such a huge difference? Am I doing something wrong in the second program? Edit: I'm compiling this with `g++ -std=c++0x -O3` on a 64-bit platform.