[英]C++ template ambiguous instantiation
我正在嘗試使用模板進行一些插值,但出現“模棱兩可的模板實例化”錯誤。 這是代碼
// 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);
}
};
基本上,我正在嘗試實施梯形規則,以便靜態展開。 但是,編譯器似乎對使用“終止案例”還是“梯形和”感到困惑。 我做錯了什么,有解決方法嗎? 如果k==0
無論InterRule
規則的類型如何,我都想強制它使用“終止案例”。
編輯附加代碼以使其運行:
// 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;
}
您可以嘗試消除添加額外模板參數的歧義。
也許像
// .................................................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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.