C 泛型設計 模板規格(譯)

2021-04-12 20:18:34 字數 4485 閱讀 7195

c++泛型設計

-模板規格(譯)

c++關鍵字template

可以用來宣告一批引數化的類(模板類)或函式(模板函式)。

一般使用格式如下:

template

< template-parameter-list > declaration

說明

template-parameter-list

是乙個以逗號分隔開的模板引數。這些模板引數可能是一些型別(以這種格式:

class

identifier

,typename

identifier

或是template

class

identifier

)或是一些非型別的引數,它們常用於模板體內,代表著實際使用時指定的型別或是非型別的物件。模板引數的語法如下之一:

parameter-declaration ²

class

identifier [ = typename ]

²typename

identifier [ = typename ]

²template

< template-parameter-list > class [identifier][= name]

你可以像例項化乙個普通類一樣去例項化乙個類模板多次,但你必須以一對尖括號(<>)來包含模板引數。這些模板引數可以是任意型別(

template-parameter-list

包含class或是typename關鍵字)或是乙個適當型別的值(

template-parameter-list

包含非型別的引數)。呼叫乙個模板函式沒有特定的語法,儘管尖括號和模板引數是必需的(當模板引數不能由引數到函式引出時)。

如下所示,

template-parameter-list

的幾個部分都是不同的,

這些引數主要用於在模板類中的函式。

template

< class t, int i > class mystack...

在這個例子中,這個模板可以接愛乙個型別(class t)和乙個常量引數(int i)。上面這個例子的模板將使用型別t和常量的整數i。在mystack宣告的類體中,你必須引用到這個識別符號t。

乙個模板的宣告不會產生**,它僅闡釋乙個類或函式的組織,當它被其它**引用時才會產生。

模板可以在全域性或是名字空間及類域下進行宣告,但不能在乙個函式內宣告。

下面這個例子演示了乙個類模板以乙個型別t及乙個非型別模板引數i進行的宣告、定義及例項化:

例項

// template_specifications1.cpp

// template class usage 1: the t is type, and the i is constant value of integer

template

class

testclass

;

template

t testclass::testfunc(t* p1)

// to create an instance of testclass

testclass classinst;

intmain()

{}

// template_specifications2.cpp

class

y

{};// template class usage 2: the pt is value of pointer

template

class

x1

{};// template class usage 3: the t2 is optional default parameter

template

class

x2

{};// to create those instances of upon classes

y ay;

x1x1;

x2 x2;

intmain()

{}

// template_specifications3.cpp

#include

// template structure usage 1

normal

template

struct

str1

;

// template structure usage 2

:the type of t can be a template class

template

class t>

struct

str2

;

intmain()

/* ---------------------- */

輸出

5

// references__supported_as_nontype_template_parameters.cpp

#include

extern

「c」

intprintf(

const

char

*, ... );

template

<

int& ri>

struct

s

// destructor method

~s()

};

inti = 1;

intmain()

/* ---------------------- */

輸出

riis 1

riis 0

非型別的模板引數

非型別的模板引數必須是:整數/列舉

/指標/引用

/指向乙個成員型別的指標(例如

template_specifications2.cpp

的x1模板類

)。正如

const

或volatile

型別一樣,在編譯時刻它們是不可改變的。另外,

float

型別的指標不能用作模板引數。儘管乙個指向

「類物件/結構

/union

型別」的指標可用作模板引數,但是它們本身卻不可用作模板引數。若傳遞的是陣列名稱,就被轉化為指標看待。若傳遞的是函式名稱,當然,是被看作為函式指標。字串是不允許看作為模板引數的。

在乙個模板宣告中使用typename

在模板引數列表中,可以使用typename關鍵字來定義乙個引數。例如下面的模板宣告是一樣的:

template

class x...

template

class x...

預設的模板引數

類模板可以擁有預設引數(指定使用

賦值操作符

賦乙個預設的型別或是乙個值)。確記,函式模板不可有預設引數。

/* 類模板預設引數用法參考簡單例子*/

template

<

typename

type>

class

allocator

{};template

<

typename

type,

typename

allocator = allocator>

class

stack

{};stack<

int> mystack;

模板引數的復用

在模板引數列表中,模板引數可以復用。如

template_specifications2.cpp

例子中的x1模板類的模板引數t在第二個引數中獲得到復用。

模板的模板引數

模板引數也可以進行模板化。這個創立前提條件是,引數本身必須是乙個模板,非乙個由模板構造好的類。在下面的例子中,模板引數的名稱a對於乙個模板的模板引數來說,可以省略,因為沒有被使用。

引用模板引數

visual studio .net 2003

引入了乙個新特性,可以引用非型別的模板引數。這個方法在以前的版本中是不允許的。

—完—

C 模板(泛型)

模板概念 模板是建立通用模具,大大提高復用性,將型別引數化。c 泛式程式設計思想,主要利用的技術就是模板 c 提供模板機制 函式模板和類模板 函式模板和類模板區別 類模板沒有自動型別推導使用方式 類模板在模板引數列表中可以有預設引數 作用 建立通用函式,其函式返回值型別和形參型別可以不具體制定,用乙...

C 泛型程式設計 模板

在學習c c 過程中,我們可能常用幾種函式,由於傳入的引數不同,需要進行不同的函式的編寫,在c語言中需要根據引數和功能的不同重新編寫新的函式,而在c 中有函式過載這樣的機制,一定程度上解決了問題,但是 過載的函式僅僅只是型別不同,的復用率比較低,只要有新型別出現時,就需要增加對應的函式 的可維護性比...

C 泛型程式設計(模板)

2.類模板 我們先來看乙個概念 泛型程式設計 使用模板,編寫和型別無關的 沒有模板之前,一些函式或者類,針對不同的型別需要寫很多重複的 函式 比如交換函式swap,假如傳入的資料型別不同,int,char,double。我們要實現不同的型別物件函式。類 比如我們像實現乙個資料結構棧stack,sta...