简体   繁体   中英

c++ extract the template arugments of a function pointer

Is there a way to extract/introspect on templated function pointer to get its template argument types and size?

More generally speaking, what kind of Callables/Invocables in C++ are able to be introspected to get the type info of template args and what are the techiniques?

Edit: What I'm trying to do is to pass a callable object to some other class as a template parameter and carrying all the info this callable has regarding what template parameters it takes. Then I invoke this callable in a more compile-time and functional friendly way.

https://godbolt.org/z/q3f98177Y

template<typename T>
T SomeFunc() {return T{};}

constexpr auto some_func_fp = &SomeFunc<int>;
// can we do some introspection to get the types 
// and size of the template arguments of some templated function pointers?

You can't, in general, the function's type (and the pointer type) has nothing to do with the template parameter. (in your case you can detect the return type though)


#include <type_traits>

template<typename T>
int SomeFunc(){return 0;}

auto fi = &SomeFunc<int>;
auto ff = &SomeFunc<float>;

// fi has same type as ff, int(*)()
static_assert(std::is_same_v<decltype(fi),decltype(ff)>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM