Type return of function pointer

I think the code will better illustrate my need:

template <typename F> struct return_type { typedef ??? type; }; 

so that:

 return_type<int(*)()>::type -> int return_type<void(*)(int,int)>::type -> void 

I know decltype and result_of , but they must have arguments. I want to infer the return type of a function pointer from a single template parameter. I cannot add the return type as a parameter, because exactly what I want to hide here ...

I know that there is a solution in boost, but I can’t use it, and trying to dig it out of boost led to an impressive crash (as is often the case).

C ++ 11 solutions are welcome (as long as they are supported in VS2012).

+8
c ++ c ++ 11 function-pointers
source share
1 answer

If you can use variadic templates (November '12 CTP), this should work:

 template <class F> struct return_type; template <class R, class... A> struct return_type<R (*)(A...)> { typedef R type; }; 

Living example .

If you cannot use variable templates, you will need to provide special specializations for parameters 0, 1, 2, ... (manually or using a preprocessor).

EDIT

As indicated in the comments, if you want to work with variational functions, you need to add one additional partial specialization (or one for each parameter in the case of non-variational templates):

 template <class R, class... A> struct return_type<R (*)(A..., ...)> { typedef R type; }; 
+9
source share

All Articles