Skip to content

Instantly share code, notes, and snippets.

@Garciat
Last active December 18, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garciat/5786154 to your computer and use it in GitHub Desktop.
Save Garciat/5786154 to your computer and use it in GitHub Desktop.
#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