2010-12-19 6 views

답변

6

"클로저 개체"인 C++ 0x lambdas는 펑터입니다. 따라서 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, ""); 
} 
+0

정확히 어떻게 할 수 있습니까? 나는 functor 자체와 그것의'operator()'에서 parameter_types, result_type 및 components를 시도했지만 작동시키지 못했습니다. – ltjax

+0

샘플의 고주파. 나는 parameter_types에서 pop'ing을 끝내고 front_type에 result_type을 추가했다. – ltjax

4

당신은 in this answer to a similar question 설명 된대로 원하는 유형을 반환하는 여러 함수를 오버로드 할 수 있습니다.

+0

좋은 링크. 또한 꽤 잘 작동하지만, 부스트 방식은 분명히 짧고 일반적인 것입니다. Upvote tho;) – ltjax

관련 문제