Skip to content

Instantly share code, notes, and snippets.

@lefticus
Last active August 29, 2015 14:06
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 lefticus/103de2c1f95fdd82621e to your computer and use it in GitHub Desktop.
Save lefticus/103de2c1f95fdd82621e to your computer and use it in GitHub Desktop.
function param deduction
#include <tuple>
#include <iostream>
#include <typeinfo>
template<typename Ret>
struct function_type
{
};
template<typename Ret>
struct function_type<Ret ()>
{
typedef Ret return_type;
typedef std::tuple<> arg_types;
static const size_t arity = 0;
};
template<typename Ret, typename Arg1>
struct function_type<Ret (Arg1)>
{
typedef Ret return_type;
typedef std::tuple<Arg1> arg_types;
static const size_t arity = 1;
};
template<typename Ret, typename Arg1, typename Arg2>
struct function_type<Ret (Arg1, Arg2)>
{
typedef Ret return_type;
typedef std::tuple<Arg1, Arg2> arg_types;
static const size_t arity = 2;
};
int main()
{
std::cout << "return type: " << typeid(function_type<void ()>::return_type).name() << std::endl;
typedef std::tuple_element<0, function_type<void (const int &)>::arg_types>::type argtype0;
std::cout << "0th arg type: " << typeid(argtype0).name() << std::endl;
std::cout << "0th arg type is ref: " << std::is_lvalue_reference<argtype0>::value << std::endl;
std::cout << "0th arg type is const: " << std::is_reference<argtype0>::value << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment