c++ check for nested typedef of a template parameter to get its scalar base type

删除回忆录丶 提交于 2019-12-05 10:46:34

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!