c++ 获取lambda参数类型

我想要一些方法来获取lambda函数的第一个参数类型,这是可能的吗?

例如

代替:

template<typename T>
struct base
{
     virtual bool operator()(T) = 0;
}

template<typename F, typename T>
struct filter : public base<T>
{
     virtual bool operator()(T) override {return /*...*/ }
};

template<typename T, typename F>
filter<T> make_filter(F func)
{
      return filter<F, T>(std::move(func));
}

auto f = make_filter<int>([](int n){return n % 2 == 0;});

我想要:

template<typename F>
struct filter : public base<typename param1<F>::type>
{
     bool operator()(typename param1<F>::type){return /*...*/ }
};

template<typename F>
filter<F> make_filter(F func)
{
      return filter<F>(std::move(func));
}

auto f = make_filter([](int n){return n % 2 == 0;});

基于Xeo的答案,这是我在VS2010中工作的:

template<typename FPtr>
struct arg1_traits_impl;

template<typename R, typename C, typename A1>
struct arg1_traits_impl<R (C::*)(A1)>{typedef A1 arg1_type;};

template<typename R, typename C, typename A1>
struct arg1_traits_impl<R (C::*)(A1) const>{typedef A1 arg1_type;};

template<typename T>
typename arg1_traits_impl<T>::arg1_type arg1_type_helper(T);

template<typename F>
struct filter : public base<typename std::decay<decltype(detail::arg1_type_helper(&F::operator()))>::type>
{
    bool operator()(typename std::decay<decltype(detail::arg1_type_helper(&F::operator()))>::type){return /*...*/ }
};

template<typename T, typename F>
filter<F> make_filter(F func)
{
      return filter<F>(std::move(func));
}

我已经尝试简化代码,但任何尝试似乎都会破坏它.

最简单的选择是使operator()成为一个模板本身:

template<typename F>
struct filter
{
     template<class Arg>
     void operator(Arg&& arg){
       // use std::forward<Arg>(arg) to call the stored function
     }
};

template<typename F>
filter<F> make_filter(F func)
{
      return filter<F>(std::move(func));
}

auto f = make_filter([](int n){return n % 2 == 0;});

现在理论上,下面的代码应该是正常的.但是,由于错误,MSVC10不支持

#include <iostream>
#include <typeinfo>

template<class FPtr>
struct function_traits;

template<class T, class C>
struct function_traits<T (C::*)>
{
    typedef T type;
};

template<class F>
void bar(F f){
  typedef typename function_traits<
      decltype(&F::operator())>::type signature;
  std::cout << typeid(signature).name();
}

int main(){
    bar([](int n){ return n % 2 == 0; });
}

Here是GCC如何看待的例子.然而,MSVC10根本不编译代码.详见this question of mine.基本上,MSVC10不会将decltype(& F :: operator())当作依赖类型.这是在chat discussion设计的一个解决方案:

#include <iostream>
#include <typeinfo>
#include <type_traits>

template<class FPtr>
struct function_traits;

template<class R, class C, class A1>
struct function_traits<R (C::*)(A1)>
{   // non-const specialization
    typedef A1 arg_type;
    typedef R result_type;
    typedef R type(A1);
};

template<class R, class C, class A1>
struct function_traits<R (C::*)(A1) const>
{   // const specialization
    typedef A1 arg_type;
    typedef R result_type;
    typedef R type(A1);
};

template<class T>
typename function_traits<T>::type* bar_helper(T);

template<class F>
void bar(F f){
  typedef decltype(bar_helper(&F::operator())) fptr;
  typedef typename std::remove_pointer<fptr>::type signature;
  std::cout << typeid(signature).name();
}

int main(){
    bar([](int n){ return n % 2 == 0; });
}
翻译自:https://stackoverflow.com/questions/8711855/get-lambda-parameter-type

转载注明原文:c++ 获取lambda参数类型