2010-12-19 4 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

私はそれをどのように正確に行いますか?ファンクタ自体とその 'operator()'の両方にparameter_types、result_type、componentsを試しましたが、動作させることができませんでした。 – ltjax

+0

サンプルのThanx。私はparameter_typesからpop'ingを終了し、フロントにresult_typeを追加しました - 魅力のように動作します! – ltjax

4

あなたはin this answer to a similar questionを説明するように、希望のタイプを返すために、いくつかの関数をオーバーロードすることができます。

+0

良いリンク。また、非常にうまくいくが、ブーストの方法は間違いなく短くて一般的だった。 Upvote tho;) – ltjax