Template Metaprogramming in C++ Last Updated : 19 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Predict the output of following C++ program. CPP #include <iostream> using namespace std; template<int n> struct funStruct { enum { val = 2*funStruct<n-1>::val }; }; template<> struct funStruct<0> { enum { val = 1 }; }; int main() { cout << funStruct<8>::val << endl; return 0; } Output: 256 The program calculates "2 raise to the power 8 (or 2^8)". In fact, the structure funStruct can be used to calculate 2^n for any known n (or constant n). The special thing about above program is: calculation is done at compile time. So, it is compiler that calculates 2^8. To understand how compiler does this, let us consider the following facts about templates and enums:1) We can pass nontype parameters (parameters that are not data types) to class/function templates. 2) Like other const expressions, values of enumeration constants are evaluated at compile time. 3) When compiler sees a new argument to a template, compiler creates a new instance of the template.Let us take a closer look at the original program. When compiler sees funStruct<8>::val, it tries to create an instance of funStruct with parameter as 8, it turns out that funStruct<7> must also be created as enumeration constant val must be evaluated at compile time. For funStruct<7>, compiler need funStruct<6> and so on. Finally, compiler uses funStruct<1>::val and compile time recursion terminates. So, using templates, we can write programs that do computation at compile time, such programs are called template metaprograms. Template metaprogramming is in fact Turing-complete, meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram. Template Metaprogramming is generally not used in practical programs, it is an interesting concept though. Comment More infoAdvertise with us Next Article is_pointer Template in C++ K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads is_pointer Template in C++ The std::is_pointer template of C++ STL is used to check whether the given type is pointer or not. It returns a boolean value showing the same. Syntax: template <class T > struct is_pointer; Parameter: This template accepts a single parameter T (Trait class) to check whether T is a pointer or 2 min read Template Specialization in C++ Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p 5 min read C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why 5 min read is_fundamental Template in C++ The is_fundamental template of C++ STL is used to check whether the type is a fundamental type or not. It returns a boolean value showing the same. Syntax: template <class T> struct is_fundamental; Parameter: This template accepts a single parameter T (Trait class) to check whether T is a fund 2 min read Partial Template Specialization in C++ In C++, template specialization enables us to define specialized versions of templates for some specific argument patterns. It is of two types: Full Template SpecializationPartial Template SpecializationIn this article, we will discuss the partial template specialization in C++ and how it is differe 3 min read is_reference Template in C++ The std::is_reference template of C++ STL is used to check whether the type is a reference type or not. It returns a boolean value showing the same. Syntax: template <class T > struct is_reference; Parameter: This template accepts a single parameter T (Trait class) to check whether T is a refe 2 min read Like