2013-04-18 16 views
7

私はBoost MPLでグリップにしようとしています。ブーストMPLネストされたラムダ

として、簡単な演習、私が試した:

typedef vector_c<int, 1, 2, 3, 4, 5>::type example_list; 

typedef transform<example_list, times<_, int_<2> > >::type doubled_example_list; 

typedef transform<example_list, negate<_> >::type negated_example_list; 

BOOST_STATIC_ASSERT((at_c<negated_example_list, 2>::type::value==-3)); 
BOOST_STATIC_ASSERT((at_c<doubled_example_list, 4>::type::value==10)); 

これらすべての作業罰金を。ただし、以下の試みはコンパイルされません:

typedef transform<_, negate<_> > negate_a_list; 

typedef apply<negate_a_list, example_list>::type negated_example_list_2; 

BOOST_STATIC_ASSERT((at_c<negated_example_list_2, 2>::type::value==-3)); 

私はしかし、私はそれを修正するかどうかはわからない、negate_a_listのプレースホルダのスコープとは何かだと思います。何か案は?また、MPLの構文とセマンティクスに関する私の前提のいくつかに欠陥があると思われます。私はMPLをgrokkingするためのヒントに感謝します。

P.S.ここでは上記のコードのためのプリアンブルがある:私の質問にリュックTourailleさんのコメントに

#include <boost/mpl/vector_c.hpp> 
#include <boost/mpl/transform.hpp> 
#include <boost/static_assert.hpp> 
#include <boost/mpl/placeholders.hpp> 
#include <boost/mpl/times.hpp> 
#include <boost/mpl/size_t.hpp> 
#include <boost/mpl/apply.hpp> 
#include <boost/mpl/lambda.hpp> 
#include <boost/mpl/negate.hpp> 
#include <boost/mpl/at.hpp> 

using namespace boost::mpl; 
using namespace boost::mpl::placeholders; 
+2

問題は、プレースホルダが2つの異なるレベルのアプリケーションを参照していることです。最初のアプリケーションは 'apply'を呼び出すときにバインドする必要があり、2つ目は' transform'を呼び出すときにバインドする必要があります。あなたのコードでは、 'negate_a_list'はバイナリメタ関数ですが、単項メタ関数を返す単項メタ関数でなければなりません。ネストされたlambdaを扱うのは難しいかもしれませんが、[Boostメーリングリストのこのスレッド](http://lists.boost.org/Archives/boost/2012/01/189614.php)でいくつかの答えを見つけることができます。 –

+0

エラッタ: 'negate_a_list'は本当に"単項メタ関数を返すべきではありません "というのは、むしろある種のカプセル化です。 (x、y)=> transform(x、y)=>(x、y)=> transform(x、y)=>変換が必要な間に、このラムダ ' )) ')。 –

答えて

5

おかげで、ブーストメーリングリストはthe answerを提供します。このコードは動作します:

typedef transform<_, lambda<negate<_> >::type > negate_a_list; 

typedef apply<negate_a_list, example_list>::type negated_example_list_2; 

BOOST_STATIC_ASSERT((at_c<negated_example_list_2, 2>::type::value==-3)); 

は、ラムダ式の周りlambda<...>::typeラッピングの追加に注意してください。これは、プレースホルダの範囲を限定するのに十分です。

関連する問題