2010-11-05 180 views
6
class Example { 

    // ... 
}; 

template <typename T, Example ex> //Error 
class MyExample{ 

    // ... 
}; 

我的問題是爲什麼不能模板非類型參數是類類型?爲什麼不能模板非類型參數是類類型

,我得到的錯誤是

error: ‘class Example’ is not a valid type for a template constant parameter

+2

請澄清 - 什麼樣的編譯器錯誤的,你看見了什麼? – seand 2010-11-05 07:31:52

+0

試試(例* ex)。它將作品:] – k06a 2010-11-05 10:53:44

+0

也許(例:EX)的作品。你試一試 。 。 。 – k06a 2010-11-05 10:54:41

回答

13

簡單,因爲這些規則。合理,模板參數具有在編譯時需要解決與類型的對象僅構造(甚至臨時對象和那些具有靜態存儲持續時間)在運行時。您只能有一個是在編譯時「值」解析如整數和類型的模板參數。儘管如此,也可以使用模板參數作爲對象的指針或引用。

3

c++ standard

A non-type template-parameter shall have one of the following (optionally cv-qualified) types: 
— integral or enumeration type, 
— pointer to object or pointer to function, 
— reference to object or reference to function, 
— pointer to member. 

A non-type template-parameter shall not be declared to have floating point, **class**, or void type. 

很明顯,如果你聲明類非類型模板參數符合標準的編譯任何性病拋出一個錯誤。

相關問題