2010-11-05 182 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. 

很明显,如果你声明类非类型模板参数符合标准的编译任何性病抛出一个错误。

相关问题