Template Metaprogramming in C++

 

  说实话,学习C++以来,第一次听说"Metaprogramming"这个名词。

  Predict the output of following C++ program.

 1 #include <iostream>
 2 using namespace std;
 3 
 4 template<int n> struct funStruct
 5 {
 6     enum { val = 2*funStruct<n-1>::val };
 7 };
 8 
 9 template<> struct funStruct<0>
10 {
11     enum { val = 1 };
12 };
13 
14 int main()
15 {
16     cout << funStruct<8>::val << endl;
17     return 0;
18 }

  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.

 

  Another one: Template metaprogram for Fibonacci number

 1 #include <iostream>
 2 using namespace std;
 3 
 4 
 5 template<int n> struct Fib 
 6 {
 7     enum { val = Fib<n-1>::val + Fib<n-2>::val };
 8 };
 9 
10 template<> struct Fib<1> 
11 { 
12     enum { val = 1 }; 
13 };
14 
15 template<> struct Fib<0> 
16 { 
17     enum { val = 0 }; 
18 };
19 
20 int main() 
21 {
22     cout << Fib<8>::val << endl;
23     
24     return 0;
25 }

  

  

  The advantages are:

  It is more of a theoretical concept. One use is optimization. The calculations done at compile time save time at run time.

 

 

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  22:29:57

转载于:https://www.cnblogs.com/iloveyouforever/p/3444454.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值