" "?
- ?
If you want to get a list of all the calls at runtime, you can simply create these functions and print something in each of them (or add to the vector or something else), for example:
int fib(int i) {
int ret;
printf("fib(%d)\n",i);
if (i>2) ret=fib(i-2)+fib(i-1);
else ret=i;
printf("fib-end(%d)\n",i);
}
If you want to get something like this, but at compile time you can try the following approach:
template <int i>
inline int fib() {
return fib<i-2>()+fib<i-1>();
}
template <>
inline int fib<0>() {return 0;}
tempalte <>
inline int fib<1>() {return 1;}
In the latter case, the Fibonacci number will be calculated at compile time, so all function calls will be expanded, and the constants will be collapsed, which will lead to a code that will be evaluated in constant time.
source
share