1

为什么这个 boost::lambda 表达式不起作用?

boost::function<bool (boost::uint64_t, boost::uint64_t&, unsigned int, float)> myFunct = boost::lambda::_3 < 1;

我得到了这些编译错误,这可能无济于事,因为它们真的很神秘。

|| In file included from /usr/include/boost/function/detail/maybe_include.hpp:33,
||                  from /usr/include/boost/function/detail/function_iterate.hpp:14,
||                  from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:67,
||                  from /usr/include/boost/function.hpp:64,
||                  from CMoteurRecherche.hpp:13,
||                  from CBenchmark.hpp:4,
||                  from CBenchmark.cpp:1:
|| /usr/include/boost/function/function_template.hpp: In static member function 'static R boost::detail::function::function_obj_invoker4<FunctionObj, R, T0, T1, T2, T3>::invoke(boost::detail::function::function_buffer&, T0, T1, T2, T3) [with FunctionObj = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<bool>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::less_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<4> >, const int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::
|| null_type, boost::tuples::null_type> > >, R = bool, T0 = long unsigned int, T1 = uint64_t&, T2 = unsigned int, T3 = float]':
/usr/include/boost/function/function_template.hpp|904| instantiated from 'void boost::function4<R, T1, T2, T3, T4>::assign_to(Functor) [with Functor = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<bool>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::less_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<4> >, const int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, R = bool, T0 = long unsigned int, T1 = uint64_t&, T2 = uns
|| igned int, T3 = float]'
/usr/include/boost/function/function_template.hpp|720| instantiated from 'boost::function4<R, T1, T2, T3, T4>::function4(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<bool>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::less_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<4> >, const int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::nul
|| l_type> > >, R = bool, T0 = long unsigned int, T1 = uint64_t&, T2 = unsigned int, T3 = float]'
/usr/include/boost/function/function_template.hpp|1040| instantiated from 'boost::function<R(T0, T1, T2, T3)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<bool>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::less_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<4> >, const int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null
|| _type> > >, R = bool, T0 = uint64_t, T1 = uint64_t&, T2 = unsigned int, T3 = float]'
/usr/include/boost/function/function_template.hpp|1081| instantiated from 'typename boost::enable_if_c<boost::type_traits::ice_not::value, boost::function<R(T0, T1, T2, T3)>&>::type boost::function<R(T0, T1, T2, T3)>::operator=(Functor) [with Functor = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<bool>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::less_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<4> >, const int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples
|| ::null_type, boost::tuples::null_type> > >, R = bool, T0 = uint64_t, T1 = uint64_t&, T2 = unsigned int, T3 = float]'
/home/mathmoi/MatMoi/CBenchmark.cpp|85| instantiated from here
/usr/include/boost/function/function_template.hpp|131| error: no match for call to '(boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<bool>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::less_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<4> >, const int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >) (long unsigned int&, long unsigned int&, unsigned int&, float&)'
/usr/include/boost/lambda/detail/lambda_functors.hpp|137| note: candidates are: typename T::sig<boost::tuples::null_type>::type boost::lambda::lambda_functor<Base>::operator()() const [with T = boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<bool>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::relational_action<boost::lambda::less_action>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<4> >, const int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> >]
4

1 回答 1

3

我认为答案来自 Boost.Lambda 参考:

在 lambda 表达式中使用占位符决定了结果函数是 nullary、unary、binary 还是 3-ary。最高的占位符索引是决定性的。

您的最高占位符是_3,但该函数需要 4 个参数。不幸的是,从引用中也可以看出,占位符最多只能提供 3 个参数。

顺便说一句,如果函数接受 3 个参数,似乎可以使用逗号运算符来显示函数的数量(结果被丢弃):

boost::function<bool(int, int, int)> f = boost::lambda::_2 < 1;  //error
boost::function<bool(int, int, int)> f = (boost::lambda::_3, boost::lambda::_2 < 1); //OK
于 2010-01-14T14:04:18.760 回答