Search code examples
c++c++11decltyperesult-of

Detecting function parameter type


I went about with the following code to detect the long argument to a given function.

So, given:

int f(int *) { return 0; }

I want to extract int *.

Here is my attempt:

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

This however does not work and gcc 4.6 gives error

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

So, I have two questions:

a) What's wrong with the above code?

b) Is it possible to do this in any other way/ways?


Solution

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