2011-10-30 11 views
0

私は、boost :: mpl :: find_ifに似たメタ関数を書こうとしていますが、最後から順番にシーケンスをたどるという違いがあります。私は私のメタ関数の引数として渡されたmpl :: lambdaの計算から来たと思いますコンパイルエラーを取得しています。私が間違っていることを指摘してくれて、とても感謝しています。mplラムダ式をテンプレート引数として渡す

今私は(元find_ifを飾る)怠惰なソリューションをしようとしている:

typedef vector_c<int, 1, 2, 3, 6, 5, 4>::type test_vect; 
typedef find<test_vect, int_<6>::type>::type it_cur; 
typedef rfind_if<test_vect, lambda<less<deref<it_cur>::type, _1> >::type >::type it_swap; 
std::cout << "it_swap=" << deref<it_swap>::type::value << "\n\n"; 

私はその不可解なエラーが出る:

#include <boost/mpl/reverse.hpp> 
#include <boost/mpl/find_if.hpp> 
#include <boost/mpl/distance.hpp> 
#include <boost/mpl/begin_end.hpp> 
#include <boost/mpl/advance.hpp> 
#include <boost/mpl/next_prior.hpp> 
#include <boost/mpl/lambda.hpp> 


using boost::mpl::reverse; 
using boost::mpl::find_if; 
using boost::mpl::distance; 
using boost::mpl::end; 
using boost::mpl::advance; 
using boost::mpl::prior; 
using boost::mpl::lambda; 

template<typename SEQ, typename pred> 
struct rfind_if { 
private: 
    // find the element in the reversed container  
    typedef typename reverse<SEQ>::type rev_SEQ; 
    typedef typename lambda<pred>::type expanded_pred;  
    typedef typename find_if<rev_SEQ, expanded_pred>::type rev_iter; 
    // compute the distance of the iterator 
    typedef typename distance<rev_iter, typename end<rev_SEQ>::type >::type dist; 
public: 
    //compute the iterator 
    typedef typename advance<typename begin<SEQ>::type, typename prior<dist>::type>::type type; 
}; 

問題は、この機能を使用しようとするということです私は、ラムダ計算から来ていると思う:

/usr/include/boost/mpl/aux_/preprocessed/gcc/less.hpp:60: error: no type named ‘tag’ in ‘struct mpl_::void_’ (some more template noise) 
/usr/include/boost/mpl/not.hpp:43: error: ‘value’ is not a member of ‘boost::mpl::aux::nested_type_wknd<boost::mpl::aux::iter_apply1 (some more template noise) 
/usr/include/boost/mpl/aux_/preprocessed/gcc/iter_fold_if_impl.hpp:62: error: no type named ‘type’ in ‘struct boost::mpl::apply2<boost::mpl::protect<boost::mpl::aux::iter_fold_if_pred (some more template noise) 
...and much more... 

私はrfiの内部をテストした

typedef vector_c<int, 1, 2, 3, 6, 5, 4>::type    test_vect; 
typedef boost::mpl::reverse<test_vect>::type    rev_SEQ; 
typedef find_if<rev_SEQ, less<int_<5>, _1> >::type   rev_iter; 
typedef distance<rev_iter, end<rev_SEQ>::type >::type  dist; 
typedef advance<begin<test_vect>::type, prior<dist>::type>::type it_begin; 

boost::mpl::for_each<rev_SEQ>(value_printer()); 

生産正しい結果

私は私の機能をはるか効率的なのであることを知っているが、今、私は問題を理解したい:(テンプレート引数としてラムダを介さずに)nd_if、それが命名、働いていました。私は後で適切な実装を書くつもりです。

よろしく

答えて

1

は、私の知る限り見るように、rfind_ifは、エラーの原因ではなく、 のコードは疑問がtest_vectend間接参照しているようです。

1) 要素の種類vector_c<int>でない int_integral_c<int>と思われます。 だからfind<test_vect, int_<6>::type>::typeendであり、test_vectです。 it_curの逆参照はderef<it_cur>::typeに無効です。

2) あなたはless<int_<6>, _1>less<deref<it_cur>::type, _1>では、test_vect以来 は、そのような要素を持っていない意味場合 は、rfind_if<...>::typeは再びtest_vectend です。 deref<it_swap>::type::valueの逆参照は無効です。

上記の問題を修正した後、 コードをideoneにコンパイルできます。

+0

あなたの説明が意味を持ち、提供されたソリューションが機能します。 – Marcin

+0

歓迎します:-) –

関連する問題