【发布时间】:2023-03-23 05:18:01
【问题描述】:
我有一系列函数{f_n} 其中f_0 是连续的,f_1 是连续可微的,$f_{n} \in C^{n}[a,b]$ 等等。我有一个 C++ 类,它通过向量 v 上的查找表对 f_n 进行数值评估
template<int n, typename Real=double>
class f
{
public:
f() { /* initialize v */ }
Real operator()(Real x) { /* find appropriate index for x, and interpolate */}
private:
std::vector<Real> v;
};
但是,如果f 是可微分的(n >= 1),我想添加一个成员函数:
template<int n, typename Real=double>
class f
{
public:
f() { /* initialize v and dv */ }
Real operator()(Real x) { /* find appropriate index for x, and interpolate on v */}
Real prime(Real x) { /* find appropriate index for x, and interpolate on dv */}
private:
std::vector<Real> v;
std::vector<Real> dv;
};
我还想为 n >= 2 添加一个二阶导数成员,依此类推。 这可以在一个班级中完成吗? (我可以接受 C++17 语法。)
【问题讨论】:
-
如何检查
n是否大于或等于1?这应该是非类型模板参数吗? -
@GuillaumeRacicot:我的错,错字。应该是
int n。 -
“这可以在一个班级中完成吗?” 嗯,不。每对模板参数都有一个不同的类。所以本质上不止一个类。您是指单个(模板)声明吗?
标签: c++ templates c++17 sfinae template-specialization