考虑下面的指数平滑器模板类。此类用于以指数方式平滑/过滤顺序数据(请参阅更新方法)。 Elemtype 可能是一个 vector ,而 Floattype 通常是一个标量。例如
ExponentialSmoother<Eigen::Vector2f, float> x(0.1, Vector2f(0.5, 0.5));
在这个例子中,第二个模板参数 Floattype 可以避免,因为 Eigen 的 Matrix 类包含一个嵌套的 typedef 来获取标量基类型:
Vector2f::Scalar
将Elemtype和Floatype都实例化为float来平滑一维数据也是合理的。在这种情况下,也可以跳过第二个模板参数。
template <class Elemtype, class Floattype>
class ExponentialSmoother
{
public:
// ctor
ExponentialSmoother(Floattype alpha, Elemtype& initial_estimate);
// getters
inline const Elemtype& getValue() const {return estimate_;}
inline const Floattype getAlpha() const {return alpha_;}
const Elemtype& update(const Elemtype& curr)
{
estimate_ = (alpha_ * curr) + (((Floattype)1-alpha) * estimate_);
return estimate_;
}
private:
Elemtype estimate_;
Floattype alpha_; // smoothing factor within [0,1]
}
现在我的问题是,仅使用一个模板参数(元素类型)实现 ExponentialSmoother 的“最优雅”解决方案是什么? 它应该适用于 Eigen vector 和矩阵,但也适用于浮点类型。
换句话说,是否可以检查 Elemtype::Scalar 是否存在,如果不存在(即 Elemtype 是 float 或 double),则将 Floattype 定义为 Elemtype?
有人问过类似的问题here .但我想知道最通用的解决方案是什么,例如,如果也应该支持 STL vector 。是否所有类型都需要相同的嵌套 typedef(或一些具有一致命名的特征类)?
最佳答案
您可以使用助手。您提供的链接几乎包含解决方案:
template<class T, class R = void>
struct enable_if_type
{
typedef R type;
};
template<class E, class Enable = void>
struct GetFloatType
{
typedef E type;
};
template<class E>
struct GetFloatType<E, typename enable_if_type<typename E::Scalar>::type>
{
typedef typename E::Scalar type;
};
然后,在你的类里面:
template <class Elemtype, class Floattype = typename GetFloatType<Elemtype>::type>
class ExponentialSmoother
{
// ...
};
此外,用户仍然可以手动提供他们的 float 类型。可以看看live .奖励:可以毫无问题地使用 C++03。
请注意,您可以添加更多 GetFloatType
的偏特化。 Here is a live example .不要忘记 ElemType
必须被GetFloatType
的唯一 特化所接受,否则它将是不明确的(并导致编译器错误).
关于C++检查模板参数的嵌套typedef以获得其标量基类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13514587/