c++11テンプレート:関数のパラメータタイプを取得する
6633 ワード
関数タイプの定義
参考資料https://stackoverflow.com/questions/9065081/how-do-i-get-the-argument-types-of-a-function-pointer-in-a-variadic-template-cla https://en.cppreference.com/w/cpp/utility/functional/function https://en.cppreference.com/w/cpp/utility/tuple/tuple_element
double(unsigned char*, unsigned char*)
を知っているとしたら、この関数定義の入力パラメータタイプと出力パラメータタイプをどのように取得しますか?c++11が提供するテンプレート関数std::function
およびstd::tuple_element
は、1つの関数で定義された入出力パラメータタイプを1つずつ解析することができ、以下は実装コードである.#include
#include
#include
#include "FSFaceSDK.h"
template<typename T>
struct function_traits;
// R
// ...Args ,
template<typename R, typename ...Args>
struct function_traits<std::function<R(Args...)>>
{
static const size_t nargs = sizeof...(Args);
//
typedef R result_type;
// ,i 0
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
inline double compare(const face_code &f1,const face_code&f2){
typedef std::function<double(unsigned char*, unsigned char*)> feacomp_fun;
return (double)FSCompare(
/* function_traits::arg<0>::type, */
(function_traits<feacomp_fun>::arg<0>::type)f1.element,
/* function_traits::arg<1>::type, */
(function_traits<feacomp_fun>::arg<1>::type)f2.element);
}
参考資料https://stackoverflow.com/questions/9065081/how-do-i-get-the-argument-types-of-a-function-pointer-in-a-variadic-template-cla https://en.cppreference.com/w/cpp/utility/functional/function https://en.cppreference.com/w/cpp/utility/tuple/tuple_element