说实话,学习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

Template Metaprogramming in C++的更多相关文章

  1. C++模板元编程(C++ template metaprogramming)

    实验平台:Win7,VS2013 Community,GCC 4.8.3(在线版) 所谓元编程就是编写直接生成或操纵程序的程序,C++ 模板给 C++ 语言提供了元编程的能力,模板使 C++ 编程变得 ...

  2. CppCon - Modern Template Metaprogramming 杂记

    2014年底才看到github和channel9上有CppCon2014的视频和资料,顿时激动不已.最近小生也一直在研习CppCon2014中令人兴奋的内容.这篇鄙文就是小生学习了<Modern ...

  3. 模板元编程(Template metaprogramming)

    https://en.wikipedia.org/wiki/Template_metaprogramming 没看懂...只知道了模板元编程的代码是在编译期运行的... 敲了2个例子: 1. #inc ...

  4. Effective C++ -----条款48:认识template元编程

    Template metaprogramming(TMP,模板元编程)可将工作由运行期移往编译期,因而得以实现早期错误侦测和更高的执行效率. TMP可被用来生成“基于政策选择组合”(based on ...

  5. 【48】认识template元编程

    1.TMP(template metaprogramming),模版元编程有两个效力:第一,它让某些事情更容易:第二,可将工作从运行期转移到编译期.

  6. 《Effective C++》:条款48:理解力template 元编程

    Template metaprogramming(TMP,模板元编程)这是写template-based C++规划.编译过程.template metaprogramming随着C++写模板程序,化 ...

  7. Metaprogramming

    Metaprogramming https://en.wikipedia.org/wiki/Metaprogramming 元编程, 是一种编程技术, 制造的计算机程序,具有这种能力, 对待程序为他们 ...

  8. C++ template —— 表达式模板(十)

    表达式模板解决的问题是:对于一个数值数组类,它需要为基于整个数组对象的数值操作提供支持,如对数组求和或放大: Array<), y(); ... x = 1.2 * x + x * y; 对效率 ...

  9. C++ template —— template metaprogram(九)

    metaprogramming含有“对一个程序进行编程”的意思.换句话说,编程系统将会执行我们所写的代码,来生成新的代码,而这些新代码才真正实现了我们所期望的功能.通常而言,metaprogrammi ...

随机推荐

  1. 转向系统的传递路径分析(Transfer Path Analysis)入门的一些分享

    分享一些自己对于<转向系统><传递路径分析>的理解 (只是一些个人理解,不涉及任何公司隐私问题,logo就懒得一个个去擦了) (1) (2) (3) (4) (5) (6) ( ...

  2. 该虚拟机似乎正在使用中。如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权

    问题 打开虚拟机镜像时报 VMware该虚拟机似乎正在使用中.如果该虚拟机未在使用,请按"获取所有权(T)"按钮获取它的所有权 解决方法 在你安装的镜像文件目录下找到后缀为.vmx ...

  3. webpack 打包html文件

    webpack 打包html文件 webpack.config.js配置文件内容为: /** * loader: 1. 下载 2. 使用(配置) * plugins:1. 下载 2. 引入 3.使用 ...

  4. Java学习(八)

    今天学了类的封装知识与编译器的使用,和c++的大体一致,只有一些细节不同,像private的使用等. 小试牛刀,写了一个封装后的类,并且测试. public class Student { priva ...

  5. 痞子衡嵌入式:在IAR开发环境下RT-Thread工程函数重定向失效分析

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是在IAR开发环境下RT-Thread工程函数重定向失效分析. 痞子衡旧文 <在IAR下将关键函数重定向到RAM中执行的方法> ...

  6. C 语言基础,来喽!

    前言 C 语言是一门抽象的.面向过程的语言,C 语言广泛应用于底层开发,C 语言在计算机体系中占据着不可替代的作用,可以说 C 语言是编程的基础,也就是说,不管你学习任何语言,都应该把 C 语言放在首 ...

  7. Linux基础四:软件包管理

    四.软件包管理器: 1.概念 红帽有两款软件包管理器,分别是rpm和yum. 1.rpm软件包管理器  ->  用来安装单个包  ->  .rpm文件 红帽的安装包文件,都放在Packag ...

  8. php简单手机商品发布系统

    原本还说学学angular2的,没想到上一公司呆了两月就走了,现在在这个公司做了一个小型的商品发布系统,,php实现的,比较简单,功能不多,是以手机模板发布商品网站的,需要的可以拿去 http://p ...

  9. SpringServletContainerInitializer的代码流程

    SpringServletContainerInitializer 是spring中的一个class实现了servlet3.0规范的一个接口 implements ServletContainerIn ...

  10. Devs--开源规则引擎介绍

    Devs Devs是一款轻量级的规则引擎. 开源地址:https://github.com/CrankZ/devs 基础概念 此规则引擎的基础概念有字段.条件.规则等. 其中字段组成条件,条件组成规则 ...