【问题标题】:What is wrong with this boost::lambda use?这个 boost::lambda 使用有什么问题?
【发布时间】:2010-01-14 13:45:39
【问题描述】:

为什么这个 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> >]

【问题讨论】:

  • 我很高兴我不再用 C++ 编写代码了 :)

标签: c++ boost boost-lambda boost-function


【解决方案1】:

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

在 lambda 中使用占位符 表达式确定是否 结果函数是零的,一元的, 二进制或 3 进制。最高的 占位符索引是决定性的。

您的最高占位符是 _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

【讨论】:

  • 嗯,看来你是对的。我不知道这个限制。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 2018-11-16
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多