[英]C++ template : no matching function for call
Consider the following code 请考虑以下代码
template <typename T, T one>
T exponentiel(T val, unsigned n) {
T result = one;
unsigned i;
for(i = 0; i < n; ++i)
result = result * val;
return result;
}
int main(void) {
double d = exponentiel<double,1.0>(2.0f,3);
cout << d << endl;
return 0;
}
The compiler tells me this no matching function for call to 'exponentiel(float, int)' 编译器告诉我这个没有匹配函数来调用'exponentiel(float,int)'
Why? 为什么?
What's strange is that exponentiel works with int. 奇怪的是exponentiel与int一起工作。
The problem is with the T one
and the 1.0
in the template argument list. 问题在于模板参数列表中的T one
和1.0
。
You can't have a nontype template parameter of a floating point type and you can't pass a floating point value as a template argument. 您不能拥有浮点类型的非类型模板参数,也不能将浮点值作为模板参数传递。 It's just not allowed (to the best of my knowledge, there's no really good reason why it's not allowed). 这是不允许的(据我所知,没有很好的理由为什么不允许)。
g++'s error message here is rather unhelpful. 这里的g ++错误信息是无益的。 Visual C++ 2010 reports the following on the line where the template is used in main
: Visual C ++ 2010在main
使用模板的行上报告以下内容:
error C2993: 'double' : illegal type for non-type template parameter 'one'
Comeau Online reports: Comeau Online报道:
line 13: error: expression must have integral or enum type
double d = exponentiel<double,1.0>(2.0f,3);
^
line 2: error: floating-point template parameter is nonstandard
template <typename T, T one>
^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.