Predict the output of following C++ program.
#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 enumaration 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 enumaration 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 conecpt though.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- std is_object Template in C++
- std is_floating_point Template in C++
- is_arithmetic Template in C++
- is_reference Template in C++
- is_lvalue_reference Template in C++
- std::is_integral template in C++
- std::is_enum Template in C++
- is_pointer Template in C++
- std is_union() template in C++
- is_class template in C++
- is_standard_layout template in C++
- is_empty template in C++
- is_final template in C++14
- is_signed template in C++
- is_polymorphic template in C++
- is_abstract template in C++
- is_pod template in C++
- is_unsigned template in C++
- is_volatile template in C++
- is_void template in C++

