6

有没有办法将 c++0x lambda 的签名、结果和参数类型推断为 Boost.MPL 序列,例如boost::mpl::vector?例如,对于 lambda

[]( float a, int b ) -> void { std::cout << a << b << std::endl; }

我想得到一个boost::mpl::vector<void,float,int>.

4

1 回答 1

6

作为“闭包对象”的 C++0x lambda 是函子。所以你可以使用 boost.Boost.FunctionTypes 来分解它的operator().

例子:

#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, "");
}
于 2010-12-19T11:18:12.007 回答