0

メンバー関数の「型」を推測する簡単な方法はありますか? 次の(メンバー)関数の型を推測したいと思います。

struct Sample {
  void func(int x) { ... }
};

void func(int x) { ... }

次のタイプに ( で使用std::function):

void(int)

引数の変数カウント (varargs ではありません!) をサポートするソリューションを探しています...

編集 - 例:

次のセマンティクスを持つ - とdecltype呼びましょう -に似た式を探しています。functiontype

functiontype(Sample::func) <=> functiontype(::func) <=> void(int)

functiontype(expr)と互換性のある型に評価される必要がありstd::functionます。

4

1 に答える 1

3

これは役に立ちますか?

#include <type_traits>
#include <functional>

using namespace std;

struct A
{
    void f(double) { }
};

void f(double) { }

template<typename T>
struct function_type { };

template<typename T, typename R, typename... Args>
struct function_type<R (T::*)(Args...)>
{
    typedef function<R(Args...)> type;
};

template<typename R, typename... Args>
struct function_type<R(*)(Args...)>
{
    typedef function<R(Args...)> type;
};

int main()
{
    static_assert(
        is_same<
            function_type<decltype(&A::f)>::type, 
            function<void(double)>
            >::value,
        "Error"
        );

    static_assert(
        is_same<
            function_type<decltype(&f)>::type, 
            function<void(double)>
            >::value,
        "Error"
        );
}
于 2013-02-18T23:02:31.897 に答える