6

考虑下面的指数平滑模板类。此类用于以指数方式平滑/过滤顺序数据(请参阅更新方法)。Elemtype 可能是一个向量,而 Floattype 通常是一个标量。例如

ExponentialSmoother<Eigen::Vector2f, float> x(0.1, Vector2f(0.5, 0.5));

在这个例子中,可以避免第二个模板参数 Floattype,因为 Eigen 的 Matrix 类包含一个嵌套的 typedef 来获取标量基类型:

Vector2f::Scalar

将 Elemtype 和 Floatype 都实例化为浮点数以平滑一维数据也是合理的。在这种情况下,也可以跳过第二个模板参数。

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 的“最优雅”的解决方案是什么?它应该适用于特征向量和矩阵,但也适用于浮点类型。

换句话说,是否可以检查 Elemtype::Scalar 是否存在,如果不存在(即 Elemtype 是 float 或 double)将 Floattype 定义为 Elemtype?

一个类似的问题已经被问到here。但是我想知道如果也应该支持 STL 向量,那么最通用的解决方案是什么。所有类型都需要相同的嵌套 typedef(或某些具有一致命名的特征类)吗?

4

1 回答 1

3

You can use a helper. The link you gave almost contains the solution:

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

Then, in your class:

template <class Elemtype, class Floattype = typename GetFloatType<Elemtype>::type>
class ExponentialSmoother
{
    // ...
};

Also, with this users can still manually provide their float type. You can see it live. Bonus: works with C++03 without problems.

Note that you can add more partial specializations of GetFloatType. Here is a live example. Don´t forget that ElemType have to be acceptable for only one specialization of GetFloatType, or else it will be ambiguous (and cause a compiler error).

于 2012-11-22T17:48:33.787 回答