Deduction type of a given pointer of a member function with variable patterns

Assuming I have a pointer to a member function, how can I write code that automatically outputs the parameters for the given signature of the template:

template<typename T> class Foo {}; template<typename R, typename C, typename... A> class Foo<R(C, A...)> { }; 

There is no problem for C and R, since something like this does the trick:

 template<typename R, typename C, typename... A> R deduce_R_type(R(C::*)(A...)); template<typename R, typename C, typename... A> C deduce_C_type(R(C::*)(A...)); 

Then I can connect it to the Foo instance, but how to deduce the types coming from the variational part of the template?

 Foo< decltype(deduce_R_type(&SomeClass::SomeFunction)) ( decltype(deduce_C_type(&SomeClass::SomeFunction)), ___ ??? ___)> instance 
+7
c ++ c ++ 11 templates decltype variadic-templates
source share
1 answer

You need at least one helper and something like std::tuple :

 template<typename R, typename C, typename... A> std::tuple<A...> deduce_A_tuple(R(C::*)(A...)); template<typename R, typename C, typename T> struct FooHelper; template<typename R, typename C, typename... A> struct FooHelper< R, C, std::tuple<A...> > { typedef Foo< R( C, A... ) > type; }; 

and then you can use:

 FooHelper< decltype(deduce_R_type(&SomeClass::SomeFunction)), decltype(deduce_C_type(&SomeClass::SomeFunction)), decltype(deduce_A_tuple(&SomeClass::SomeFunction)) >::type instance; 

Live example


As a DyP user pointed out, this can be a lot easier if you should still use the helper anyway:

 template<typename> struct FooHelper; template<typename R, typename C, typename... A> struct FooHelper< R (C::*)(A...) > { using type = Foo< R( C, A... ) >; }; 

and use it as

 FooHelper< decltype(&SomeClass::SomeFunction) >::type instance; 
+7
source share

All Articles