2014-09-12 2 views
0

У меня есть boost :: function pointer's хранится в std :: map. Это указывает на лямбда-функции. Как я могу получить возвращаемый тип?Тип возвращаемого значения функции лямбда-функции

#include "main.h" 
#include <typeinfo> 

typedef std::map<std::string,boost::function<int (A*)>> str_func_map; 

int main() 
{ 
    str_func_map mapping; 

    mapping["One"] = [](A *a) {return a->one();}; 
    mapping["Two"] = [](A *a) {return a->two();}; 
    mapping["B_Nine"] = [](A *a) {return a->getB().nine();}; 

    A aa = A(); 
    A* a = &aa; 

    for (str_func_map::iterator i = mapping.begin(); i != mapping.end(); i++) 
    { 
     std::cout<< i->first << std::endl; 
     std::cout<< (i->second)(a) << std::endl; 
     typedef decltype(i->second) type; //How can I print out the return type of 
     //the function pointer??? 


    } 
    system("pause"); 
} 
+0

erm не всегда ли int? – Nim

ответ

1

boost::functionstd::function а) имеют вложенную ЬурейеЕ return_type. Так просто использовать, что:

typedef decltype(i)::return_type TheReturnType; 

// or indeed 

typedef str_func_map::mapped_type::return_type TheReturnType; 

Конечно, в вашем случае, это будет int.

+0

Мне интересно, хотят ли они распечатать «тип». В этом случае они должны были бы развязать типид; (в gcc это делается https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html) – kingtorus

Смежные вопросы