Question : Template Metaprogramming

Hello I just sow this code in wiki and was curious about something.

template
struct Factorial
{
    enum { value = N * Factorial::value };
};
 
template <>
struct Factorial<0>
{
    enum { value = 1 };
};
 
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}

What if I put Factorial<100> and then I compile.

Is it defining all the templates from 1 to 100?

After that in run-time I supply a value of 24.

If yes, will it use the already defined template for 24 or will it evaluate it again from the beginning?

It should use the defined template of 24 and immediately give me the result without any more calculations but I have to be certain.

Answer : Template Metaprogramming

Yes, yes, no.   A template is fixed once instantiated.   There is no computation at runtime.  You are merely doing "x=4 ;x=24;"

Random Solutions  
 
programming4us programming4us