繁体   English   中英

C ++模板:没有匹配的调用函数

[英]C++ template : no matching function for call

请考虑以下代码

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;
}

编译器告诉我这个没有匹配函数来调用'exponentiel(float,int)'

为什么?

奇怪的是exponentiel与int一起工作。

问题在于模板参数列表中的T one1.0

您不能拥有浮点类型的非类型模板参数,也不能将浮点值作为模板参数传递。 这是不允许的(据我所知,没有很好的理由为什么不允许)。

这里的g ++错误信息是无益的。 Visual C ++ 2010在main使用模板的行上报告以下内容:

error C2993: 'double' : illegal type for non-type template parameter 'one'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM