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
Template Metaprogramming in C++的更多相关文章
- C++模板元编程(C++ template metaprogramming)
实验平台:Win7,VS2013 Community,GCC 4.8.3(在线版) 所谓元编程就是编写直接生成或操纵程序的程序,C++ 模板给 C++ 语言提供了元编程的能力,模板使 C++ 编程变得 ...
- CppCon - Modern Template Metaprogramming 杂记
2014年底才看到github和channel9上有CppCon2014的视频和资料,顿时激动不已.最近小生也一直在研习CppCon2014中令人兴奋的内容.这篇鄙文就是小生学习了<Modern ...
- 模板元编程(Template metaprogramming)
https://en.wikipedia.org/wiki/Template_metaprogramming 没看懂...只知道了模板元编程的代码是在编译期运行的... 敲了2个例子: 1. #inc ...
- Effective C++ -----条款48:认识template元编程
Template metaprogramming(TMP,模板元编程)可将工作由运行期移往编译期,因而得以实现早期错误侦测和更高的执行效率. TMP可被用来生成“基于政策选择组合”(based on ...
- 【48】认识template元编程
1.TMP(template metaprogramming),模版元编程有两个效力:第一,它让某些事情更容易:第二,可将工作从运行期转移到编译期.
- 《Effective C++》:条款48:理解力template
元编程
Template metaprogramming(TMP,模板元编程)这是写template-based C++规划.编译过程.template metaprogramming随着C++写模板程序,化 ...
- Metaprogramming
Metaprogramming https://en.wikipedia.org/wiki/Metaprogramming 元编程, 是一种编程技术, 制造的计算机程序,具有这种能力, 对待程序为他们 ...
- C++ template —— 表达式模板(十)
表达式模板解决的问题是:对于一个数值数组类,它需要为基于整个数组对象的数值操作提供支持,如对数组求和或放大: Array<), y(); ... x = 1.2 * x + x * y; 对效率 ...
- C++ template —— template metaprogram(九)
metaprogramming含有“对一个程序进行编程”的意思.换句话说,编程系统将会执行我们所写的代码,来生成新的代码,而这些新代码才真正实现了我们所期望的功能.通常而言,metaprogrammi ...
随机推荐
- char* 和 char[] 的区别
一.代码 有关下面代码,p和q的区别是什么: int main(int argc, char *argv[]) { char* p = "Hello World"; char q[ ...
- 流媒体技术的应用,如何搭建一个SimpleNVR流媒体服务系统
Onvif/RTSP流媒体服务 SimpleNVR Onvif/RTSP流媒体服务是一款软硬一体音视频流媒体服务软件.它是在5G.AI.云计算.大数据.物联网等网络技术大规模商用后,用户要求视频随时随 ...
- 攻防世界 WEB 高手进阶区 NSCTF web2 Writeup
攻防世界 WEB 高手进阶区 NSCTF web2 Writeup 题目介绍 题目考点 php基本函数语法 加密解密函数 base64_decode().str_rot13() 字符串反转函数 str ...
- TDSQL | 在整个技术解决方案中HTAP对应的混合交易以及分析系统应该如何实现?
从主交易到传输,到插件式解决方案,每个厂商对HTAP的理解和实验方式都有自己的独到解法,在未来整个数据解决方案当中都会往HTAP中去牵引.那么在整个技术解决方案中HTAP对应的混合交易以及分析系统应该 ...
- JMeter学习笔记--关联
1.什么是关联? 本次请求需要的数据,需要上一步的请求返回给提供的过程. 2.JMeter关联中常用的两种方式 正则表达式提取器 正则表达式提取器用于对页面任何文本的提取,提取的内容是根据正则表达式在 ...
- Java开发介绍之JDK JRE JVM 和 环境变量配置
一.JDK>JRE>JVM JDK(Java Development Kit):Java开发工具包 JDK中包含JRE,在JDK的安装目录下有一个名为jre的目录,里面有两个文件夹bin和 ...
- 浅讲.Net 6 并与之前版本写法对比
介绍 昨天vs2022正式版已经推出了,估计很多人已经下载并开始创建.Net 6 开始尝鲜了, 本节我简要的给大家介绍一下.Net 6的一些改动. 正文 本次.Net6带来最明显的变化就是: 采用顶级 ...
- Azure Virtual NetWoker(一)对等互连网络
一,引言 Virtual NetWork Peering 可以无缝连接两个 Azure Virtual NetWork,Virtual NetWork Peering 直接的网络流量是专用的.在实际项 ...
- vue中this.$set的用法
之前了解这个方法的时候,感觉这一辈子也用不到这个方法,因为当时没有应用场景,但是还真有用的时候,我相信你们也有用到时候. 从三个方面给大家说一下这个this.$set: 1.this.$set实现什么 ...
- 基于WPF的酷炫GUI窗口的实现全过程
title: 基于WPF的酷炫GUI窗口的实现全过程 date: 2020-08-14 permalink: /build/wpfgui sidebarDepth: 2 tags: wpf gui 软 ...