template <typename T>
void handle(T &hl)
{
typedef decltype(&T::operator()) foo_type;
std::cout << typeid(boost::mpl::at_c<boost::function_types::parameter_types<foo_type>,2>::type).name();
}
handle([](int a, char c){});
lambda其实是个C++对象(函数对象),只不过里面包含int operator()(int a,int b)之类的成员函数,从而可以被当做函数来使用,因此lambda表达式被当做参数来传递时,其实是传递的C++对象
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())> //利用继承的方式 ,&T::operator()指向类成员函数的指针
{};
// For generic types, directly use the result of the signature of its 'operator()'
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> //这里处理得太妙了,这个是function_traits的特化版
// we specialize for pointers to member function
{
enum { arity = sizeof...(Args) };
// arity is the number of arguments.
typedef ReturnType result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
// the i-th argument is equivalent to the i-th tuple element of a tuple
// composed of those arguments.
};
};
// test code below:
int main()
{
auto lambda = [](int i) { return long(i*10); };
typedef function_traits<decltype(lambda)> traits;
static_assert(std::is_same<long, traits::result_type>::value, "err");
static_assert(std::is_same<int, traits::arg<0>::type>::value, "err");
return 0;
}