64,677
社区成员
发帖
与我相关
我的任务
分享
template<class T, char *p>
void f(){ }
f<int, 0>(); // error
f<int, (char*)0>(); // error: 除了转换为整型或者enum,其它的类型转换不能用在常量表达式中
char *const p = 0;
f<int, p>(); // error: p不能出现在一个常量表达式中
f<int, "haha">(); // error: "haha"不是一个合法的模板实参,因为字符串字面值永远不能用在这种上下文中。
f<int, (char*)0>(); // error: 除了转换为整型或者enum,其它的类型转换不能用在常量表达式中
char *const p = 0;
f<int, p>(); // error: 对于p,必须有常量值
f<int, "haha">(); // error: "haha"一个模板实参不能引用一个非外部实体
extern char g_s[] = "haha";
int main(void)
{
f<int, NULL>(); // 在VC++2008中可以通过
f<int, g_s>(); //因为你的模板形参原本没有const修饰,所以这里外部变量g_s也不用const修饰。
// 如果用了const修饰的话需要进行显示的类型转换
}
template<char const* p>
class ET{};
extern char const s[] = "good";
ET<s> F;