Skip to content

Instantly share code, notes, and snippets.

@i-saint
Created February 19, 2025 13:58
Show Gist options
  • Save i-saint/f8fa7ecd1a220b73806363efb08ce301 to your computer and use it in GitHub Desktop.
Save i-saint/f8fa7ecd1a220b73806363efb08ce301 to your computer and use it in GitHub Desktop.
func_traits
template <typename>
struct func_traits;
// function
template <typename R, typename... Args>
struct func_traits<R (*)(Args...)>
{
using return_type = R;
using args_type = std::tuple<Args...>;
template <int I>
using arg_type = std::tuple_element_t<I, args_type>;
};
// member function
template <typename R, typename C, typename... Args>
struct func_traits<R (C::*)(Args...)>
{
using return_type = R;
using callee_type = C;
using args_type = std::tuple<Args...>;
template <int I>
using arg_type = std::tuple_element_t<I, args_type>;
};
// const member function
template <typename R, typename C, typename... Args>
struct func_traits<R (C::*)(Args...) const>
{
using return_type = R;
using callee_type = const C;
using args_type = std::tuple<Args...>;
template <int I>
using arg_type = std::tuple_element_t<I, args_type>;
};
// functor
template <typename C>
struct func_traits
{
using _traits = func_traits<decltype(&C::operator())>;
using return_type = typename _traits::return_type;
using callee_type = typename _traits::callee_type;
using args_type = typename _traits::args_type;
template <int I>
using arg_type = std::tuple_element_t<I, args_type>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment