Last active
August 29, 2015 14:06
-
-
Save lefticus/103de2c1f95fdd82621e to your computer and use it in GitHub Desktop.
function param deduction
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 <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