我試圖編寫一個類似於boost :: mpl :: find_if的元函數,但區別在於它將遍歷從結尾開始的序列。我得到的編譯錯誤,我認爲來自計算的mpl :: lambda作爲我的元函數的參數傳遞。我會非常感謝任何關於我做錯事的指針。傳遞一個mpl lambda表達式作爲模板參數
現在我想一個懶惰的解決方案(裝飾原find_if):
#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;
};
的問題是,當嘗試使用此功能:
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";
我得到神祕的錯誤,這些錯誤,我猜,來自lambda計算:
/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的內部nd_if(不通過lambda作爲一個模板參數)和它的工作,命名:
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());
產生正確的結果
我知道,我的功能還遠遠沒有效率,但現在我想了解的問題。之後我會寫一個適當的實施。
問候
謝謝,您的解釋是有道理的,並提供解決方案。 – Marcin
不客氣:-) –