C ++模板模棱两可的实例化

问题描述 投票:0回答:1

我正在尝试使用模板进行一些插值,但出现“模棱两可的模板实例化”错误。这是代码

// interpolation rules
enum InterRule {trap, rect, trapSum};

// Rectangle rule
template <int n, int k, InterRule rule, class Expr> struct Integration {
    static double integrate(double a, double b){
        return (b-a)/n * Expr::eval(a + (k-1)*(b-a)/n) + Integration<n, k - 1, rule, Expr>::integrate(a,b);
    }
};

// termination case
template <int n, InterRule rule, class Expr> struct Integration<n,0,rule,Expr> {
    static double integrate(double a, double b){
        return 0;
    }
};

// Trapezoidal rule
template <int n, int k, class Expr> struct Integration<n, k, trap, Expr> {
    static double integrate(double a, double b){
        return (b-a)/n * (Expr::eval(a)/2 + Integration<n,k-1,trapSum,Expr>::integrate(a,b) + Expr::eval(b)/2);
    }
};

// Trapezoidal sum
template <int n, int k, class Expr> struct Integration<n, k, trapSum, Expr> {
    static double integrate(double a, double b){
        return Expr::eval(a + k*(b-a)/n) + Integration<n,k-1,trapSum,Expr>::integrate(a,b);
    }
};

[基本上,我正在尝试实施梯形规则,以便将其静态展开。enter image description here但是,似乎编译器对于使用“终止大小写”还是“梯形和”感到困惑。我做错了什么,有什么解决方法?无论k==0规则的类型如何,如果InterRule,我都想强制使用“终止大小写”。

EDIT使它运行的其他代码:

// the types of expressions (+,-,*, etc.)
enum ExprType { mul, divide, add, sub, constant};

// constant
template <ExprType eType, class Left, class Right, int coeff, int power> struct Expr {
    static double eval(double x){
        return coeff * std::pow(x, power);
    }
};


int main()
{

    double a = 1;
    double b = 2;

    // Expr defines the function f(x) = x
    Integration<50, 50, trap, Expr<constant,int,int,1,1>> inte2;
    std::cout << inte2.integrate(a,b) << std::endl;

    return 0;
}
c++ templates variadic-templates template-meta-programming template-specialization
1个回答
1
投票

您可以尝试消除添加其他模板参数的歧义。

也许是

// .................................................VVVVVVVVVVV
template <int n, int k, InterRule rule, class Expr, bool = true>
struct Integration {
   // ...
};

[false显式的案例

template <int n, InterRule rule, class Expr>
struct Integration<n, 0, rule, Expr, false> { // <--- false !
    static double integrate(double a, double b){
        return 0;
    }
};

并在递归调用中添加正确的参数

    return (b-a)/n * Expr::eval(a + (k-1)*(b-a)/n)
       + Integration<n, k - 1, rule, Expr, (k>1)>::integrate(a,b);
    // ....................................^^^^^

以下是完整的编译示例

#include <cmath>
#include <iostream>

// interpolation rules
enum InterRule {trap, rect, trapSum};

// Rectangle rule
template <int n, int k, InterRule rule, class Expr, bool = true>
struct Integration {
    static double integrate(double a, double b){
        return (b-a)/n * Expr::eval(a + (k-1)*(b-a)/n)
           + Integration<n, k - 1, rule, Expr, (k>1)>::integrate(a,b);
    }
};

// termination case
template <int n, InterRule rule, class Expr>
struct Integration<n, 0, rule, Expr, false> {
    static double integrate(double a, double b){
        return 0;
    }
};

// Trapezoidal rule
template <int n, int k, class Expr>
struct Integration<n, k, trap, Expr> {
    static double integrate(double a, double b){
        return (b-a)/n * (Expr::eval(a)/2
           + Integration<n,k-1,trapSum,Expr,(k>1)>::integrate(a,b)
           + Expr::eval(b)/2);
    }
};

// Trapezoidal sum
template <int n, int k, class Expr>
struct Integration<n, k, trapSum, Expr> {
    static double integrate(double a, double b){
        return Expr::eval(a + k*(b-a)/n)
           + Integration<n,k-1,trapSum,Expr,(k>1)>::integrate(a,b);
    }
};

// the types of expressions (+,-,*, etc.)
enum ExprType { mul, divide, add, sub, constant};

// constant
template <ExprType eType, class Left, class Right, int coeff, int power> struct Expr {
    static double eval(double x){
        return coeff * std::pow(x, power);
    }
};


int main ()
 {

    double a = 1;
    double b = 2;

    // Expr defines the function f(x) = x
    Integration<50, 50, trap, Expr<constant,int,int,1,1>> inte2;
    std::cout << inte2.integrate(a,b) << std::endl;
 }
© www.soinside.com 2019 - 2024. All rights reserved.