Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

c++ - Is there a way to deduce the signature of a lambda as an mpl sequence?

Is there a way to deduce the signature, result- and parameter-types, of a c++0x lambda as a Boost.MPL sequence, for example a boost::mpl::vector? For example, for a lambda

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

I would like to get a boost::mpl::vector<void,float,int>.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

C++0x lambdas which are "closure-objects" are functors. So you can use boost.Boost.FunctionTypes to decompose its operator().

Example:

#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, "");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...