青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

模版元編程(Template Meta Programming )

 

I.背景

C
++在很多人的心目中,一直是一種OO語言,而事實上,現在對C++的非OO部分的各種使用被逐漸地挖掘出來,其中最大的部分莫過于是 template。STL、loki、boost,,很多先行者為我們提供了方案,有的已經被列入C++標準的一部分。template的一個重要使用方法就是template meta programming,它利用編譯器對于template的解釋是靜態的這一特性,讓編譯器在編譯時做計算,可以有效的提高程序的運行速度。有關于 template meta programming的記載,最早見于Erwin Unruh,他在1994年寫了一個用template計算質數的程序。我希望通過這篇文章介紹一些TMP的基本技巧和應用,并且最終完成一個質數計算程序。
(閱讀本文的過程中,建議你試圖編譯每一個給出的程序。由于所有的類只需要public成員,所以都用struct聲明,但是仍然稱之為類。)

II.技術

通常我們編寫一個(小)程序,需要的語言支持其實不必很多,只要有順序、選擇和循環三種控制結構理論上就可以寫出大多數程序了。我們先用TMP建立一個簡單的語言環境。

1.打印

程序有了結果,需要有一個方式反饋給運行者,這里我們利用C
++的出錯信息,建立一個打印函數。要知道我們希望一切都在編譯的時候結束,那么我們就必須讓C++編譯器在編譯信息里面告訴我們,所以我們利用編譯器的出錯信息。當然這只是一個trick,如果你的TMP只是程序的一部分,你可以使用正常的輸入輸出。

template
<unsigned int value>
struct print
{
static 
const unsigned int result = (unsigned char*)value;
};

這個類,每當別人引用到它的result的時候,編譯器就會打印出錯信息,因為一個unsigned int是不能隱式的轉成一個unsigned char
*的。譬如下面這段程序

template
<unsigned int value>
struct print
{
static 
const unsigned int result = (unsigned char*)value;
};

unsigned 
int test1 = print<77>::result;
unsigned 
int test2 = print<123>::result;

在我的Dev C
++里,會輸出

main.cpp: In instantiation of `print
<77>':
main.cpp:7: instantiated from here
main.cpp:
4: invalid conversion from `unsigned char*' to `unsigned int'
main.cpp: In instantiation of `print<123>':
main.cpp:8: instantiated from here
main.cpp:
4: invalid conversion from `unsigned char*' to `unsigned int'

這個輸出雖然不是很好看,但也算是差強人意。

2.選擇

Andrei Alexanderescu在他的大作Modern C
++ Design里面使用過一個類,可以根據bool的值選擇不同的類型。今天我們要寫的一個是根據bool的值選擇不同的整數。

template
<bool condition, unsigned int value1, unsigned int value2>
struct template_if
{
static 
const unsigned int result = value1;
};

template
<unsigned int value1, unsigned int value2>
struct template_if
<false, value1, value2>
{
static 
const unsigned int result = value2;
};

這里用到了模板的特化,如果你對這個不熟悉,那么大致可以這樣理解:第一個template_if的定義告訴編譯器,“一般的” template_if,會選擇第一個值作為結果。第二個template_if告訴編譯器,如果第一個參數是false的話,我們就使用第二個值(第三個參數)作為結果。下面這段代碼演示了template_if的用法。

template
<unsigned int value>
struct print
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<bool condition, unsigned int value1, unsigned int value2>
struct template_if
{
static 
const unsigned int result = value1;
};

template
<unsigned int value1, unsigned int value2>
struct template_if
<false, value1, value2>
{
static 
const unsigned int result = value2;
};

template
<unsigned int value>
struct print_if_77
{
static 
const unsigned int result = template_if<value == 77 , print<value>::result , 0>::result;
};

unsigned 
int test1 = print_if_77<77>::result;
unsigned 
int test2 = print_if_77<123>::result;

如果你去編譯這段代碼的話,你會發覺77和123都被打印出來了,雖然錯誤信息不一樣,但是這不是我們想要的結果。為什么呢?很遺憾,對C
++編譯器來說,template_if<true1100>和template<true1200>是兩個不同的類,雖然后一個參數的值我們并不關心,但是編譯器必須在template初始化的時候,給出所有的參數,這就導致它會去計算 print<value>::result,當然,計算的結果就是報錯。也就是說,因為編譯器要計算這個值才導致了我們的print不可用,要解決這個問題,有兩個方法:或者讓編譯器不計算這個值,或者讓編譯器在某些情況下可以計算出正確的值。

方法一可以讓編譯器不計算這個值,通過修改template_if,我們傳入兩個不同的類,而不是unsigned 
int
首先修改print,加一個新的類dummy_print:

template
<unsigned int value>
struct print
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<unsigned int value>
struct dummy_print
{
static 
const unsigned int result = value;
};

接著,加入一套對類型進行選擇的模板:

template
<bool condition, typename T1, typename T2>
struct template_if_type
{
static 
const unsigned int result = T1::result;
};

template
<typename T1, typename T2>
struct template_if_type
<false, T1, T2>
{
static 
const unsigned int result = T2::result;
};

這樣原先的程序就變成:

template
<unsigned int value>
struct print
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<unsigned int value>
struct dummy_print
{
static 
const unsigned int result = value;
};

template
<bool condition, typename T1, typename T2>
struct template_if_type
{
static 
const unsigned int result = T1::result;
};

template
<typename T1, typename T2>
struct template_if_type
<false, T1, T2>
{
static 
const unsigned int result = T2::result;
};

template
<unsigned int value>
struct print_if_77
{
static 
const unsigned int result = template_if_type<value == 77 ,
 dummy_print
<value> , print<value>>::result;
};

void main()
{
unsigned 
int test1 = print_if_77<77>::result;
//unsigned int test2 = print_if_77<123>::result;
}

現在的“運行結果”非常正確。

方法二可以讓編譯器在某些情況下計算出正確的值,我們加一套新的模板:

template
<bool condition, unsigned int value>
struct print_if
{
static 
const unsigned int result = value;
};

template
<unsigned int value>
struct print_if
<false, value>
{
static 
const unsigned int result = (unsigned char*)value;

};

原先的程序變為:
template
<bool condition, unsigned int value>
struct print_if
{
static 
const unsigned int result = value;

};

template
<unsigned int value>
struct print_if
<false, value>
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<unsigned int value>
struct print_if_77
{
static 
const unsigned int result = print_if<value == 77 , value>::result;
};

void main()
{
unsigned 
int test1 = print_if_77<77>::result;
//unsigned int test2 = print_if_77<123>::result;
}


輸出也是正確的。

這兩種方案,我個人傾向于后者,因為其實我們一定是要做一次判斷的,并且這次判斷一定會添加新的類,那么還是print_if的解決方案比較直觀。

3. 循環

首先必須明確的是,template不可能實現我們一般意義上的循環,但是它可以做一件和循環類似的事情:迭代。
如果有這樣一個循環:
for( unsigned int i = 0 ; i < value ; ++i )
我們可以這樣寫:

template
<unsigned int value>
struct 
loop
{
static 
const unsigned int result = loop<value - 1>::result + 1;
};

template
<>
struct 
loop<0>
{
static 
const unsigned int result = 0;
};

這就是告訴編譯器,我們的迭代從0開始,到value結束,每個值是前者加1。
下面給出一個更廣泛的循環的實現:
for( unsigned int i = begin ; i < end ; i = i + step ),假設0<=begin<end,并且step>0。(更復雜的情況,總可以通過template specialization分派完成)

template
<unsigned int begin, unsigned int end, unsigned int step, bool loop_continue = begin < end >
struct 
loop
{
static 
const unsigned int result = loop< begin + step, end, step>::result - step;
};

template
<unsigned int begin, unsigned int end, unsigned int step>
struct 
loop<begin, end, step, false>
{
static 
const unsigned int result = begin;
};

這里的result的計算過程不重要,關鍵是為了驅動編譯器進一步的實例化模板。
下面是一個實例程序,用來打印13到29之間的整數,步長為5。

template
<bool condition, unsigned int value>
struct print_if
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<unsigned int value>
struct print_if
<false, value>
{
static 
const unsigned int result = value;
};

template
<unsigned int begin, unsigned int end, unsigned int step, bool loop_continue = begin < end >
struct 
loop
{
static 
const unsigned int result = loop< begin + step, end, step>::result - step;
static 
const unsigned int print_result = print_if<true, result>::result;
};

template
<unsigned int begin, unsigned int end, unsigned int step>
struct 
loop<begin, end, step, false>
{
static 
const unsigned int result = begin;
};

static unsigned 
int result = loop<13,29,5>::result;

III.應用

上面我已經介紹了怎樣用TMP實現打印,選擇和循環了,現在我們來把這些投入運用。下面我會用上面的所提供的機制,寫兩個程序:計算階乘和計算質數。

1.計算階乘

我們先寫一個普通的C
++程序來計算階乘:

#include 
<iostream>

int main()
{
unsigned 
int limit = 10;
unsigned 
int factorial = 1;
for( unsigned int i = 1 ; i <= limit ; ++ i )
factorial 
*= i;
std::cout
<<factorial<<std::endl;
}

這個程序是一個一重循環,我們就用循環來做:

template
<unsigned int value>
struct print
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<unsigned int begin, unsigned int end, unsigned int step, bool loop_continue = begin < end >
struct 
loop
{
static 
const unsigned int result = loop< begin + step, end, step>::result * begin;
};

template
<unsigned int begin, unsigned int end, unsigned int step>
struct 
loop<begin, end, step, false>
{
static 
const unsigned int result = begin;
};

static unsigned 
int result = print<loop<1101>::result>::result;

因為這里不必要盤算是否輸出,所以就直接用print了。

2.計算質數

同樣,我們先寫一個普通的程序來計算:

#include 
<iostream>

int main()
{
unsigned 
int limit = 30;
for( unsigned int i = 2 ; i <= limit ; ++i )
{
unsigned 
int j;
for( j = 2 ; j < i ; ++j )
if( i % j == 0 )
break;
if( i == j )
std::cout
<<i<<std::endl;
}
}

這里用到了兩層循環,而且還是用了分支和打印。用我們提供的機制轉化成TMP形式如下:

template
<bool condition, unsigned int value>
struct print_if
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<unsigned int value>
struct print_if
<false, value>
{
static 
const unsigned int result = value;
};

template
<bool condition, unsigned int value1, unsigned int value2>
struct template_if
{
static 
const unsigned int result = value1;
};

template
<unsigned int value1, unsigned int value2>
struct template_if
<false, value1, value2>
{
static 
const unsigned int result = value2;
};

// 這里增加一個i作為參數,因為在內循環也需要知道外部的i的值

template
<unsigned int i, unsigned int begin, unsigned int end, unsigned int step, bool loop_continue = begin < end >
struct inner_loop
{
static 
const unsigned int result = template_if<i % begin,
inner_loop
< i, begin + step, end, step>::result,
begin
>::result;
};

template
<unsigned int i, unsigned int begin, unsigned int end, unsigned int step>
struct inner_loop
<i, begin, end, step, false>
{
static 
const unsigned int result = begin;
};

template
<unsigned int begin, unsigned int end, unsigned int step, bool loop_continue = begin < end >
struct outer_loop
{
static 
const unsigned int result = outer_loop< begin + step, end, step>::result;
static 
const unsigned int is_prime = inner_loop<begin, 2, begin, 1>::result == begin;
static 
const unsigned int print_result = print_if<is_prime, begin>::result;
};

template
<unsigned int begin, unsigned int end, unsigned int step>
struct outer_loop
<begin, end, step, false>
{
static 
const unsigned int result = 0;
};

static unsigned 
int result = outer_loop<2301>::result;

III.細節

另外有兩點要說一下:
我們的template_if其實有一種更簡單的寫法,就是?:表達式。
而我們的print_if和print其實可以用確省的模板參數來統一,唯一的區別是,要把value放在condition前面。

template
<unsigned int value, bool condition = true>
struct print
{
static 
const unsigned int result = (unsigned char*)value;
};

template
<unsigned int value>
struct print
<value,false>
{
static 
const unsigned int result = value;
};

這樣你可以用print
<value>來打印一個數值,也可以用print<value, condition>來做判斷打印。

IIII.后記

很久以前看Inside OLE2的時候,記得作者說過一句話:作者因為寫書而明白。我其實幾年前就寫過類似的程序,但是從來沒有對這樣程序的寫法進行過總結以至于每一次都是在重新開始。而寫完這篇文章后,我覺得自己比過去明白很多。template meta programming還有很多不同的應用,我以后有機會會繼續介紹給大家。
對于這篇文章有任何問題,請發信到 polyrandom@hotmail.com 和我聯系,也請訪問 http:
//www.allaboutprogram.com/ 以獲得最近的更新。

posted on 2007-03-05 10:33 夢在天涯 閱讀(5204) 評論(2)  編輯 收藏 引用 所屬分類: CPlusPlus

評論

# re: 模版元編程(Template Meta Programming ) 2007-03-06 09:10 gql

程序好像寫亂了吧  回復  更多評論   

# re: 模版元編程(Template Meta Programming )[未登錄] 2007-04-30 16:50 recorder

暈S,看得頭大,建議看榮耀在c++大會上的演講PPT《C++模板元編程技術與應用》,那個容易理解一些。  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811735
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              久久狠狠久久综合桃花| 亚洲国产欧美不卡在线观看| 亚洲无线视频| 国产精品私拍pans大尺度在线| 亚洲自拍偷拍福利| 亚洲免费在线看| 激情综合中文娱乐网| 欧美顶级少妇做爰| 欧美精品v日韩精品v国产精品| 亚洲天堂久久| 先锋影音久久久| 亚洲国产综合91精品麻豆| 亚洲欧洲在线播放| 国产拍揄自揄精品视频麻豆| 欧美成人一区二免费视频软件| 欧美日本高清视频| 久久精品盗摄| 欧美日韩美女一区二区| 久久av一区二区| 欧美成人一区二区三区片免费| 亚洲欧美日韩另类精品一区二区三区| 性欧美暴力猛交另类hd| 亚洲精一区二区三区| 亚洲欧美国产一区二区三区| 亚洲国产成人不卡| 亚洲一级一区| 亚洲毛片播放| 久久成人亚洲| 亚洲欧美日韩中文视频| 免费高清在线视频一区·| 午夜亚洲性色福利视频| 欧美va天堂| 久久精品综合| 欧美亚一区二区| 亚洲电影av在线| 国产一区香蕉久久| 日韩视频精品在线| 亚洲国产日韩在线| 亚洲欧美一区二区三区在线| 一本色道久久综合亚洲精品高清| 欧美一区在线直播| 中文精品在线| 欧美精品日日鲁夜夜添| 欧美jizz19性欧美| 国产日韩欧美在线一区| 亚洲天堂免费观看| 一区二区三区导航| 欧美大片专区| 美女精品国产| 影音先锋成人资源站| 性色av香蕉一区二区| 午夜免费电影一区在线观看| 欧美香蕉大胸在线视频观看| 日韩午夜在线观看视频| 亚洲精品五月天| 免费观看成人网| 欧美韩日一区二区| 亚洲第一区在线| 久久精品人人做人人爽| 久久精品亚洲| 国产综合色产| 久久精品国产亚洲a| 久久综合九九| 在线成人激情黄色| 久久亚洲美女| 亚洲高清123| 夜夜嗨一区二区三区| 欧美日韩一区二区免费在线观看| 亚洲精品欧美日韩| 亚洲一本大道在线| 国产精品国产精品| 亚洲欧美视频在线观看视频| 久久久夜精品| 亚洲高清在线播放| 欧美理论电影在线播放| 一区二区免费在线观看| 欧美在线一二三| 在线不卡欧美| 欧美精品久久99久久在免费线| 免费欧美电影| 玖玖精品视频| 久久精品中文字幕一区| 国产欧美一区在线| 99av国产精品欲麻豆| 亚洲综合不卡| 狠狠噜噜久久| 欧美福利一区| 亚洲女优在线| 亚洲大片av| 亚洲欧美日韩系列| 国产一区二区三区丝袜| 欧美 日韩 国产 一区| 中国成人在线视频| 久久亚洲精品网站| 一本色道久久综合狠狠躁篇怎么玩 | 亚洲成人在线视频网站| 99re66热这里只有精品4| 国产精品私人影院| 美国三级日本三级久久99| 一区二区免费在线视频| 久久香蕉国产线看观看av| 一本色道久久综合亚洲精品不卡 | 国产精品色一区二区三区| 久久久在线视频| 一区二区国产日产| 欧美mv日韩mv亚洲| 午夜精品视频在线| 亚洲免费激情| 国产一区二区三区丝袜| 欧美日韩岛国| 久久一综合视频| 亚洲欧美精品中文字幕在线| 91久久夜色精品国产九色| 久久激情婷婷| 亚洲欧美激情诱惑| 亚洲精品日韩激情在线电影| 国语精品中文字幕| 国产精品久久久久影院亚瑟| 欧美国产日韩一区二区三区| 欧美综合国产| 亚洲欧美一区二区视频| 99精品国产在热久久婷婷| 亚洲二区在线视频| 美国成人毛片| 久久久久国产一区二区| 欧美一区二区三区播放老司机| 日韩视频一区二区在线观看| 亚洲第一视频网站| 黄网站色欧美视频| 韩国三级电影久久久久久| 国产日韩欧美一区在线 | 在线免费观看日本欧美| 国产一区二区三区免费观看| 国产精品视频精品| 国产精品v欧美精品∨日韩| 欧美日韩无遮挡| 欧美巨乳波霸| 欧美天堂亚洲电影院在线播放| 欧美日韩国产成人| 欧美日韩国产页| 欧美精品久久久久久久| 欧美激情国产精品| 欧美欧美天天天天操| 欧美另类综合| 欧美视频在线观看免费网址| 欧美午夜宅男影院在线观看| 欧美日韩免费一区| 国产精品成人一区二区| 国产精品久久久久av| 国产欧美1区2区3区| 国产亚洲精品成人av久久ww| 国产亚洲欧美另类中文| 好吊妞**欧美| 亚洲免费观看视频| 亚洲一卡久久| 久久国产综合精品| 你懂的网址国产 欧美| 欧美激情区在线播放| 亚洲精品视频在线观看免费| 亚洲亚洲精品在线观看| 欧美一区二区三区播放老司机| 久久中文欧美| 欧美日韩国产在线播放| 国产伦精品一区二区三区四区免费| 国产欧美一区二区三区在线老狼| 樱桃成人精品视频在线播放| 艳女tv在线观看国产一区| 亚洲欧美影音先锋| 久久综合狠狠综合久久综合88 | 欧美高清在线一区二区| 日韩视频不卡| 久久成人av少妇免费| 欧美高清视频一区二区| 国产精品永久入口久久久| 亚洲高清三级视频| 亚洲欧美激情视频| 美女网站久久| 中文久久精品| 欧美xxxx在线观看| 国产精品视频一| 亚洲日本欧美| 久久精品免费播放| 日韩视频在线观看免费| 久久久久久亚洲精品杨幂换脸| 欧美揉bbbbb揉bbbbb| 永久91嫩草亚洲精品人人| 亚洲香蕉网站| 亚洲国产清纯| 久久精品国产成人| 国产精品久久久久久户外露出| 亚洲国产国产亚洲一二三| 欧美伊久线香蕉线新在线| 亚洲欧洲精品一区二区三区波多野1战4 | 亚洲一区二区三区免费在线观看| 久久久久网址| 国产欧美激情| 亚洲欧美日韩爽爽影院| 亚洲毛片网站| 欧美电影美腿模特1979在线看| 韩日精品视频|