C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c检查模板参数的嵌套typedef以获取其标量基类型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
虑下面的指数平滑模板类.此类用于以指数方式平滑/过滤顺序数据(请参阅更新方法). 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(或具有一致命名的某些Traits类)?

解决方法

你可以使用帮手.您提供的链接几乎包含解决方案:
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
{
    // ...
};

此外,有了这个用户仍然可以手动提供他们的浮动类型.你可以看到它live.奖金:与C 03一起使用没有问题.

请注意,您可以添加更多GetFloatType的部分特化. Here is a live example.别忘了ElemType只能接受GetFloatType的一个特化,否则它将是不明确的(并导致编译器@L_618_19@).

大佬总结

以上是大佬教程为你收集整理的c检查模板参数的嵌套typedef以获取其标量基类型全部内容,希望文章能够帮你解决c检查模板参数的嵌套typedef以获取其标量基类型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。