2016-03-28 7 views
0

これは、クラスメンバーメソッドの署名を保持するためのテンプレートです。これにはデフォルトの実装はありませんが、メソッドが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; 
}; 
+0

ん[この](http://stackoverflow.com/questions/14926482/const-and-non -const-template-specialization)はまったく役に立ちますか? – Matt

+0

多分、私はどのように見ることができません。私はクラス内のcvに関する情報を保存し、それを消去しないことを望みます。 – user2807083

答えて

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

作成したすべてのクラスがこのクラスを継承している場合は、多くのコード行と繰り返しを減らすことができます。

Btw多くの修飾子がありません.Cスタイルのバリデーションとrvalueを考慮すると、約30オーバーロードが必要です。ここで

はgithubのから取った、私のコードからの例です:ここhttp://coliru.stacked-crooked.com/a/03bff2946f1d3097

オリジナルコード:https://github.com/gracicot/kangaru/blob/master/include/kangaru/detail/function_traits.hpp

関連する問題