2014-09-12 144 views
0

我将boost :: function指针存储在std :: map中。这些指向lambda函数。我如何获得这些返回类型?返回类型的lambda函数指针

#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 2014-09-12 15:29:29

回答

1

boost::function(和std::function以及)有一个嵌套的typedef return_type。所以只是使用:

typedef decltype(i)::return_type TheReturnType; 

// or indeed 

typedef str_func_map::mapped_type::return_type TheReturnType; 

当然,在你的情况下,这将是int

+0

我想知道他们是否要打印'类型'。在这种情况下,他们将不得不取消该typeid; (在gcc中这是完成https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html) – kingtorus 2014-09-12 17:38:55