如何最小化模板代码?

时间:2016-03-28 15:33:39

标签: c++ templates

这是用于保存类成员方法签名的模板。它没有默认实现,但是当方法具有或者没有c样式的可变参数并且具有或没有cv限定符的所有组合时,它们都适用于每种情况。所有这些东西产生了8个非常相似的代码片段。 任何人都可以建议缩小此模板的方法:

#include <type_traits>
template <typename ... args>
struct params_t
{
    // ...
};

template <typename T>
struct mem_fn_t;

template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... )>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... );
    typedef typename std::remove_pointer<pfunction>::type function;
};


template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... ) const>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... ) const;
    typedef typename std::remove_pointer<pfunction>::type function;
};

template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... ) volatile>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... ) volatile;
    typedef typename std::remove_pointer<pfunction>::type function;
};

template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... ) const volatile>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... ) const volatile;
    typedef typename std::remove_pointer<pfunction>::type function;
};

template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... , ... )>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... , ... );
    typedef typename std::remove_pointer<pfunction>::type function;
};


template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... , ... ) const>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... , ... ) const;
    typedef typename std::remove_pointer<pfunction>::type function;
};

template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... , ... ) volatile>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... , ... ) volatile;
    typedef typename std::remove_pointer<pfunction>::type function;
};

template <typename class_t, typename ret_val_t, typename ... args>
struct mem_fn_t<ret_val_t (class_t::*)(args ... , ... ) const volatile>
{
    typedef class_t class_type;
    typedef ret_val_t result_type;
    typedef params_t<args...> params_type;
    typedef result_type (class_type::*pfunction)(args ... , ... ) const volatile;
    typedef typename std::remove_pointer<pfunction>::type function;
};

1 个答案:

答案 0 :(得分:2)

只需使用普通继承。

template<typename C, typename R, typename... Args>
struct base_mem_fn_t {
    using class_type = C;
    using result_type = R;
    using params_type = params_t<Args...>;
};

如果您创建的每个类都继承了这个类,那么可以减少很多行代码和重复。

顺便说一下,有很多限定符缺失,如果考虑到c风格的变量和r值,你应该有大约30重载。

以下是我的代码示例,取自github:http://coliru.stacked-crooked.com/a/03bff2946f1d3097

原始代码:https://github.com/gracicot/kangaru/blob/master/include/kangaru/detail/function_traits.hpp