2010-12-19 21 views

Respuesta

6

C++ 0x lambdas que son "objetos de cierre" son funtores. Entonces puede usar boost.Boost.FunctionTypes para descomponer su operator().

Ejemplo:

#include <boost/function_types/parameter_types.hpp> 

#include <boost/mpl/at.hpp> 
#include <boost/mpl/int.hpp> 

int main() 
{ 
    int x = 1; 
    auto f = [x](char a, short b, int c){ return x; }; 

    typedef decltype(f) lambda_t; 
    typedef boost::function_types::parameter_types< 
     decltype(&lambda_t::operator())>::type args_t; 
    // we can use boost::mpl::identity<decltype(f)>::type instead of lambda_t 

    static_assert(sizeof(boost::mpl::at<args_t, boost::mpl::int_<1>>::type) == 1, ""); 
} 
+0

¿Cómo exactamente eso haría? Probé parameter_types, result_type y components, tanto en el propio functor como en su 'operator()', pero no pude hacerlo funcionar. – ltjax

+0

Gracias por la muestra. Terminé pop'ing de parameter_types y agregué result_type al frente - ¡funciona como un hechizo! – ltjax

4

Puede sobrecargar varias funciones para devolver el tipo deseado como se describe in this answer to a similar question.

+0

Buen enlace. También funciona bastante bien, pero la forma de impulso fue definitivamente más corta y más genérica. Upvote tho;) – ltjax

Cuestiones relacionadas