Can I get the Owning Object of a Member Function Template Parameter?

  • A+
Category:Languages

Given a object:

struct foo {     void func(); }; 

Now given the templatized function declaration:

template<typename T, T F> void bar(); 

So bar will be taking in a member function like so:

bar<decltype(&foo::func), &foo::func>() 

In the body of bar I want to recover the type foo from T. Can I do that? I want to be able to do something like this:

get_obj<T> myfoo;  (myfoo.*F)(); 

I know that get_obj isn't a thing, but would there be a way to write it?

 


If you restrict to void(T::mem_fun)():

#include <iostream> struct foo {     void func(){ std::cout << "foo"; } };  template <typename T> struct get_type; template <typename T> struct get_type<void(T::*)()> {     using type = T; }; template <typename T> using get_type_t = typename get_type<T>::type;   template<typename T, T F> void bar(){     get_type_t<T> myfoo;     (myfoo.*F)(); }  int main () {     bar<decltype(&foo::func), &foo::func>(); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: