有这样的代码:常变量不能作为参考模板参数,但非const可以
#include <iostream>
template<const double& f>
void fun5(){
std::cout << f << std::endl;
}
//const double dddd = 5.0; error: ‘dddd’ cannot appear in a constant-expression
//however it works for extern const double dddd = 5.0;
double dddd = 5.0; // works
int main()
{
fun5<dddd>();
return 0;
}
const double dddd
不作为模板参数(但extern const double dddd
作品)工作。 double dddd
的作品,但它不是恒定的。在全球范围内定义的extern const double
和const double
变量有什么区别?
我认为这是重复的,我们在不久前。要点是,'const'全局改变了与内部或某事的联系,并且模板参数在C++ 11之前需要外部链接。 – Xeo 2012-02-09 22:08:25
so'double dddd = 5.0;'有外部联系呢? – scdmb 2012-02-09 22:10:58
看到马克B的回答,是的。 – Xeo 2012-02-09 22:12:41