関数型を取得するには、部分的な特殊化を使用できます。
template <typename T>
struct Detect;
template <typename F>
struct Detect<std::function<F>> {
typedef F Result;
};
不明なstd::function<?>
タイプを取得したら、T
使用するだけです
typename Detect<T>::Result
(一部のコンテキスト (フィールド型など) では関数へのポインタのみが許可され、生の関数型は許可されないため、 as としてResult
定義することをお勧めします。F *
編集:
引数の数と最初の型に特化するには、C++11 可変個引数テンプレートのいずれかが必要です。
template <typename T>
struct Detect;
template <typename R, typename A, typename... As>
struct Detect<std::function<R(A,As...)>> {
static constexpr auto numargs = 1 + sizeof...(As);
typedef R Result;
typedef A FirstArg;
};
または、考えられる引数の数ごとに個別の特殊化を使用して、上記と同等のコードを作成します。
template <typename R, typename A1>
struct Detect<std::function<R(A1)>> {
enum { numargs = 1 };
typedef R Result;
typedef A1 FirstArg;
};
template <typename R, typename A1, typename A2>
struct Detect<std::function<R(A1,A2)>> {
enum { numargs = 2 };
...
};
...