[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppTemplateMetaprogramming
Example to calculate the factorial:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppTemplateMetaprogramming
(C++) Template metaprogramming
Making programs that are executed at compile time. For this it uses templates to derive types or values at compile time.Example to calculate the factorial:
template <unsigned int N> struct factorial { static unsigned const value = N * factorial<N-1>::value; }; template <> struct factorial<0> { static unsigned const value = 1; }; int main() { std::cout << factorial<0>::value << std::endl << factorial<1>::value << std::endl << factorial<2>::value << std::endl << factorial<3>::value << std::endl << factorial<4>::value << std::endl << factorial<5>::value << std::endl << factorial<6>::value << std::endl << factorial<7>::value << std::endl << factorial<8>::value << std::endl << factorial<9>::value << std::endl << factorial<10>::value << std::endl << factorial<11>::value << std::endl << factorial<12>::value << std::endl; }
- include <iostream>
External links
- WikiPedia: http://en.wikipedia.org/wiki/Template_metaprogramming
- The CodeProject, 'A gentle introduction to Template Metaprogramming with C++' by moliate: http://www.codeproject.com/cpp/crc_meta.asp
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
