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

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

问题描述

考虑下面的指数平滑的模板类。这个类是平滑/过滤指数序列数据(参见更新方法)。 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因为本征的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浮平滑的三维数据。在这种情况下,第二模板paramater也可以被跳过。

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最优雅的解决方案?
它应该与特征向量和矩阵而且还与浮点类型的工作。

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 ::标量的存在,如果没有(即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?

有一个类似的问题已经被问<一个href=\"http://stackoverflow.com/questions/9644477/how-to-check-whether-a-class-has-specified-nested-class-defination-or-typedef-in\">here.但我想知道最通用的解决方案是什么,如果比如STL的载体也应得到支持。将所有类型的要求相同的嵌套的typedef(或某些性状一致的命名类)?

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

然后,在你的类:

Then, in your class:

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

另外,在该用户仍然可以手动提供他们的float类型。你可以看到它现场。奖励:与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天全站免登陆