返回lambda函数指针的类型

时间:2014-09-12 15:18:49

标签: c++ boost lambda

我有一个存储在std :: map中的boost :: function指针。这些指向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");
}

1 个答案:

答案 0 :(得分: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