有这样的代码:参考模板参数功能模板
#include <iostream>
template<const double& f>
void fun5(){
std::cout << f << std::endl;
}
int main()
{
const double dddd = 5.0;
fun5<dddd>();
return 0;
}
编译过程中编译器错误:
$ g++ klasa.cpp -o klasa
klasa.cpp: In function ‘int main()’:
klasa.cpp:11:10: error: ‘dddd’ cannot appear in a constant-expression
klasa.cpp:11:16: error: no matching function for call to ‘fun5()’
klasa.cpp:11:16: note: candidate is:
klasa.cpp:4:6: note: template<const double& f> void fun5()
为什么将“DDDD”作为模板参数不工作,应该做什么使其工作?
你不需要使它成为'extern';正如你所说,它可以有内部联系。 – 2012-02-09 21:08:31
@MikeSeymour:从理论上讲,是的,但二者gcc和铛拒绝,如果'extern'省略:( – kennytm 2012-02-09 21:12:43
克++ 6.1接受 '的extern const的双DDDD = 5.0;' 或只是 '双DDDD = 5.0;',然而“常量双DDDD = 5.0;”不被接受(在全局区所有这些定义在功能主要) – scdmb 2012-02-09 21:14:13