There is this code:
#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 doesn’t work as template parameter (however extern const double dddd works). double dddd works, but it is not constant. What is difference between extern const double and const double variables defined in global scope?
constgives variables internal linkage by default, while bothexternand non-const do not. Internal linkage symbols are expressly prohibited from being template parameters by the standard (pre-C++11, I know a few things changed there).