当前位置: 首页 > 图文教程 > 开发语言 > VC++ > C++模板元编程

VC++
如何实现24位色工具条
如何定制浮动工具条
如何实现工具栏的下拉箭头按钮
Outlook式样界面菜单和页面控制
如何在状态栏中实现进度指示器控制
Outlook风格的单列使用不同的颜色显示新邮件数
为CListBox加上智能水平滚动条
如何实现类似VC属性表中的钉子按钮
在窗体中加入3D Bar
通过程序向RichEditView写文本
如何在工具栏上添加平面下拉控件
按数据库记录构建树控件
树控件的双击响应
VC++中轻松实现滑动控件
深入浅出 CPropertySheet
如何在ListBox中选择背景位图
在对话框中加入属性页
如何在EDITBOX控件中使用背景位图
如何在树型控件中使用背景位图
树型控件拖动的完美实现

VC++ 中的 C++模板元编程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 81 ::
收藏到网摘: n/a

C++模板元编程
作者:荣耀

提交者:eastvc 发布日期:2003-12-14 19:50:43
原文出处:http://www.royaloo.com/articles/articles_2003/Metaprogramming.htm


摘要

本文描述了模板元编程技术的起源、概念和机制,并介绍了模板元编程技术在Blitz++和Loki程序库中的应用。 

关键字

编译期计算  模板元编程  Blitz++  Loki 

导言 

1994年,C++标准委员会在圣迭哥举行的一次会议期间Erwin Unruh展示了一段可以产生质数的代码。这段代码的特别之处在于质数产生于编译期而非运行期,在编译器产生的一系列错误信息中间夹杂着从2到某个设定值之间的所有质数:
 
// Prime number computation by Erwin Unruh 
template <int i> struct D { D(void*); operator int(); }; 

template <int p, int i> struct is_prime { 
    enum { prim = (p%i) && is_prime<(i > 2 ? p : 0), i -1> :: prim }; 
}; 

template < int i > struct Prime_print { 
    Prime_print<i-1> a; 
    enum { prim = is_prime<i, i-1>::prim }; 
    void f() { D<i> d = prim; } 
}; 

struct is_prime<0,0> { enum {prim=1}; }; 
struct is_prime<0,1> { enum {prim=1}; }; 
struct Prime_print<2> { enum {prim = 1}; void f() { D<2> d = prim; } }; 
#ifndef LAST 
#define LAST 10 
#endif 
main () { 
    Prime_print<LAST> a; 


类模板D只有一个参数为void*的构造器,而只有0才能被合法转换为void*。1994年,Erwin Unruh采用Metaware 编译器编译出错信息如下(以及其它一些信息,简短起见,它们被删除了): 
| Type `enum{}′ can′t be converted to txpe `D<2>′ ("primes.cpp",L2/C25). 
| Type `enum{}′ can′t be converted to txpe `D<3>′ ("primes.cpp",L2/C25). 
| Type `enum{}′ can′t be converted to txpe `D<5>′ ("primes.cpp",L2/C25). 
| Type `enum{}′ can′t be converted to txpe `D<7>′ ("primes.cpp",L2/C25). 
如今,上面的代码已经不再是合法的C++程序了。以下是Erwin Unruh亲手给出的修订版,可以在今天符合标准的C++编译器上进行编译:
 
// Prime number computation by Erwin Unruh 

template <int i> struct D { D(void*); operator int(); }; 

template <int p, int i> struct is_prime { 
    enum { prim = (p==2) || (p%i) && is_prime<(i>2?p:0), i-1> :: prim }; 
}; 

template <int i> struct Prime_print { 
Prime_print<i-1> a; 
    enum { prim = is_prime<i, i-1>::prim }; 
    void f() { D<i> d = prim ? 1 : 0; a.f();} 
}; 

template<> struct is_prime<0,0> { enum {prim=1}; }; 
template<> struct is_prime<0,1> { enum {prim=1}; }; 

template<> struct Prime_print<1> { 
    enum {prim=0}; 
    void f() { D<1> d = prim ? 1 : 0; }; 
}; 

#ifndef LAST 
#define LAST 18 
#endif 

main() { 
    Prime_print<LAST> a; 
    a.f(); 

在GNU C++ (MinGW Special) 3.2中编译这段程序时,编译器将会给出如下出错信息(以及其它一些信息,简短起见,它们被删除了): 
Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 17]'
Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 13]'
Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 11]'
Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 7]'
Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 5]'
Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 3]'
Unruh.cpp:12: initializing argument 1