将mpl lambda表达式作为模板参数传递



我正试图编写一个类似于boost::mpl::find_if的元函数,但不同之处在于它将遍历从末尾开始的序列。我收到了编译错误,我想这些错误来自作为元函数参数传递的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 << "nn";

我得到了一些神秘的错误,我想,这些错误来自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...

我已经测试了rfind_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() );

产生正确的结果

我知道我的功能远没有效率,但现在我想了解这个问题。之后我将编写一个适当的实现。

向致以最诚挚的问候

据我所见,rfind_if不是错误的原因,而是该问题似乎与CCD_ 3的CCD_。

1(vector_c<int>中元素的类型似乎是integral_c<int>,而不是int_。所以find<test_vect, int_<6>::type>::type就是test_vectend。因此,在deref<it_cur>::type中取消引用it_cur是无效的。

2(如果你用CCD_ 13来指CCD_,由于test_vect没有这样的元素,因此rfind_if<...>::type再次是CCD_ 17的CCD_。因此,在deref<it_swap>::type::value中取消引用它是无效的。

在解决了上述问题之后,代码可以在ideone上编译。

最新更新