【问题标题】:Passing a mpl lambda expression as a template argument将 mpl lambda 表达式作为模板参数传递
【发布时间】:2011-10-30 20:38:31
【问题描述】:

我正在尝试编写一个类似于 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 << "\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...

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

产生了正确的结果

我知道我的功能远非高效,但现在我想了解问题所在。之后我会写一个适当的实现。

最好的问候

【问题讨论】:

    标签: c++ boost lambda boost-mpl


    【解决方案1】:

    据我所知,rfind_if 不是错误的原因,而是代码 这个问题似乎取消了对test_vectend 的引用。

    1) vector_c&lt;int&gt; 中元素的类型似乎是integral_c&lt;int&gt;,而不是 int_。 所以find&lt;test_vect, int_&lt;6&gt;::type&gt;::typetest_vectend。 因此在deref&lt;it_cur&gt;::type 中解引用it_cur 是无效的。

    2) 如果你的意思是less&lt;int_&lt;6&gt;, _1&gt;less&lt;deref&lt;it_cur&gt;::type, _1&gt;, 因为test_vect 没有这样的元素,所以rfind_if&lt;...&gt;::type 是 又是test_vectend。 所以在deref&lt;it_swap&gt;::type::value 中取消引用是无效的。

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

    【讨论】:

    • 谢谢,您的解释很有道理,提供的解决方案有效。
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 2012-12-27
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    相关资源
    最近更新 更多