Is it possible to figure out the parameter type and return type of a lambda?

后端 未结 4 1811
[愿得一人]
[愿得一人] 2020-11-22 02:08

Given a lambda, is it possible to figure out it\'s parameter type and return type? If yes, how?

Basically, I want lambda_traits which can be used in fol

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 02:26

    Though I'm not sure this is strictly standard conforming, ideone compiled the following code:

    template< class > struct mem_type;
    
    template< class C, class T > struct mem_type< T C::* > {
      typedef T type;
    };
    
    template< class T > struct lambda_func_type {
      typedef typename mem_type< decltype( &T::operator() ) >::type type;
    };
    
    int main() {
      auto l = [](int i) { return long(i); };
      typedef lambda_func_type< decltype(l) >::type T;
      static_assert( std::is_same< T, long( int )const >::value, "" );
    }
    

    However, this provides only the function type, so the result and parameter types have to be extracted from it. If you can use boost::function_traits, result_type and arg1_type will meet the purpose. Since ideone seems not to provide boost in C++11 mode, I couldn't post the actual code, sorry.

提交回复
热议问题