考虑代码:错误时明确地将模板非类型参数
class Base{};
class Derived: public Base{};
template<Base& b> // references (and pointers) can be used as non-types
void f(){}
int main()
{
Derived d;
// f(d); // Error, template type must match exactly
f<(Base&)d>(); // Error here, why?!
}
我明白为什么评论调用失败:模板类型必须完全匹配。但是我尝试在第二个电话打石膏,并得到这个错误(gcc5.2):如果我做Derived d;
全球
error: 'd' is not a valid template argument for type 'Base&' because it is not an object with external linkage
同样的错误。铿锵有点更有帮助,说
... note: candidate template ignored: invalid explicitly-specified argument for template parameter 'b'
我的问题是:是否代码上面的法律或不?如果不是,有什么理由为什么?
另请参阅http://stackoverflow.com/questions/5687540/non-type-template-parameters – DrWatson