2

次のコードを使用して、特定の関数への長い引数を検出しました。

したがって、与えられた:

int f(int *) { return 0; }

抽出したいint *

これが私の試みです:

template<class T, class U> struct SingleArg {
    typedef U MyArg;
};

template<class T, class U> SingleArg<T, U> fT(T (*p)(U));

int main() {
    std::result_of<decltype(fT(f))>::type::MyArg t;
}

ただし、これは機能せず、gcc 4.6 でエラーが発生します

> error: std::result_of<SingleArg<int, int*> >::type has not been
> declared

だから、私は2つの質問があります:

a) 上記のコードの何が問題になっていますか?

b) 他の方法でこれを行うことは可能ですか?

4

3 に答える 3

5
#include <type_traits>

template <typename Function>
struct arg_type;

template <class Ret, class Arg>
struct arg_type<Ret(Arg)> {
  typedef Arg type;
};


int f(int *) {
  return 0;
};

int main(int, char**) {
  static_assert(std::is_same<int*, arg_type<decltype(f)>::type>::value, "different types");
}
于 2012-11-30T14:26:01.410 に答える
0
int f(int *) { return 0; }

template<class T, class U> struct SingleArg {
    typedef U MyArg;
};


template<typename T> 
struct the_same_type
{
 typedef T type;   
};

template<class T, class U> SingleArg<T, U> fT(T (*p)(U));

int main() {

    int k;
    the_same_type<decltype(fT(f))>::type::MyArg t= &k;
    return 0;
}
于 2012-11-30T13:35:24.277 に答える
0

これは私のために働く:

// if you want the type
typedef decltype(fT(f))::MyArg theType;

// if you want the name (may need demangling depending on your compiler)
std::cout << typeid(decltype(fT(f))::MyArg).name() << std::endl;

デマングリングについては、eg を参照してくださいabi::__cxa_demangle

于 2012-11-30T13:15:49.103 に答える