|
-
November 22nd, 2008, 01:15 PM
#1
Template metaprogramming calculations
Hello,
Recently I am trying to learn something new, and I found out that compilation time calculations would be very useful for me quite often. So, I grabbed my copy of google, looked up some sites, and came with this example:
Code:
#include <iostream>
template <int N>
struct Fibonacci {
enum {value = Fibonacci<N-1>::value + Fibonacci<N-2>::value};
};
template <>
struct Fibonacci<1> {
enum {value = 1};
};
template <>
struct Fibonacci<0> {
enum {value = 0};
};
int fibonacci(int which) {
//WHAT GOES HERE?
}
int main() {
std::cout << "15th Fibonacci number is "<< Fibonacci<15>::value << '\n';
int which;
std::cout << "Which Fibonacci number you want to calculate? ";
std::cin >> which;
std::cout << which << ". Fibonacci number is " << fibonacci(which) << '\n';
}
Simple as it is, I seem to miss something basic. The thing is, I just do not get how all these precalculated values can help me at runtime. I do not know how to "map" variable value to precalculated template class.
What I do not know yet? What am I doing wrong? Are my expectations that I can somehow "bind" runtime values with ones calculated by metaprogram wrong?
Cheers,
Maciek
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|