Printing factorial at compile time in C++

Posted by user519882 on Stack Overflow See other posts from Stack Overflow or by user519882
Published on 2011-02-12T11:11:57Z Indexed on 2011/02/13 7:25 UTC
Read the original article Hit count: 197

Filed under:
|
|
template<unsigned int n>
struct Factorial {
    enum { value = n * Factorial<n-1>::value};
};

template<>
struct Factorial<0> {
    enum {value = 1};
};

int main() {
    std::cout << Factorial<5>::value;
    std::cout << Factorial<10>::value;
}

above program computes factorial value during compile time. I want to print factorial value at compile time rather than at runtime using cout. How can we achive printing the factorial value at compile time?

I am using VS2009.

Thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates