Last active
December 18, 2015 12:58
-
-
Save Garciat/5786154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <tuple> | |
#include <string> | |
#include <typeinfo> | |
#include <memory> | |
#include <cxxabi.h> | |
namespace extra { | |
template <typename... Types> | |
struct TypeInfoExtractor; | |
template <typename Head, typename... Types> | |
struct TypeInfoExtractor<Head, Types...> { | |
template <typename Func> | |
static void extract(const Func &f) { | |
f(typeid(Head)); | |
TypeInfoExtractor<Types...>::extract(f); | |
} | |
}; | |
template <> | |
struct TypeInfoExtractor<> { | |
template <typename Func> | |
static void extract(const Func &f) { | |
// nop | |
} | |
}; | |
template <typename T> | |
struct function_traits; | |
template <typename Return, typename... Params> | |
struct function_traits<Return(Params...)> { | |
static const size_t arity = sizeof...(Params); | |
typedef Return return_type; | |
typedef std::tuple<Params...> parameter_tuple; | |
template <typename Func> | |
static void extract_type_info(const Func &f) { | |
TypeInfoExtractor<Params...>::extract(f); | |
} | |
}; | |
} | |
void f(int, int, float, char*, char[10]) { } | |
int main() { | |
typedef extra::function_traits<decltype(f)> f_traits; | |
f_traits::extract_type_info([] (const std::type_info &rtti) { | |
std::unique_ptr<char, void(*)(void*)> realname(abi::__cxa_demangle(rtti.name(), 0, 0, nullptr), free); | |
std::cout << realname.get() << std::endl; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment