c ++检查模板参数的嵌套typedef以获取其标量基本类型 [英] c++ check for nested typedef of a template parameter to get its scalar base type

查看:249
本文介绍了c ++检查模板参数的嵌套typedef以获取其标量基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的指数平滑器模板类。此类用于按顺序数据进行指数平滑/滤波(请参阅update方法)。 Elemtype可能是一个向量,Floattype通常是一个标量。例如

Consider the exponential smoother template class below. This class is for smoothing/filtering sequential data exponentially (see update method). Elemtype might be an vector and Floattype is usually a scalar. E.g.

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

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

In this example the second template parameter Floattype could be avoided because Eigen's Matrix class contains a nested typedef to get the scalar base type:

Vector2f::Scalar

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

It is also reasonable to instantiate both Elemtype and Floatype as floats to smooth one dimensional data. In this case the second template paramater could also be skipped.

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向量和矩阵一起使用,但也可以与浮点类型一起使用。

Now my question is what is the "most elegant" solution to implement the ExponentialSmoother with only one template parameter (the element type)? It should work with Eigen vectors and matrices but also with floating point types.

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

In other words, is it possible to check if Elemtype::Scalar exists and if not (i.e. Elemtype is float or double) define the Floattype as Elemtype?

一个类似的问题已被问过此处。但是我想知道什么是最通用的解决方案是如果例如STL向量应该也支持。所有类型都需要相同的嵌套typedef(或一些具有一致命名的traits类)?

A similar question has been asked here. But I am wondering what the most generic solution is if for instance STL vectors should be supported as well. Would all types require the same nested typedef (or some traits class with consistent naming)?

推荐答案

您提供的链接几乎包含了解决方案:

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

然后,在您的课程中:

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

此外,使用此用户仍然可以手动提供其float类型。您可以直播。 Bonus:使用C ++ 03没有问题。

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

请注意,您可以添加更多的部分专业化 GetFloatType 这是一个实例。不要忘记 ElemType 只能用于 专用化 GetFloatType 否则它将是不明确的(并导致编译器错误)。

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).

这篇关于c ++检查模板参数的嵌套typedef以获取其标量基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆