2013-10-24 53 views
0

假设我有以下代码:如何写的boost :: lambda表达式作为函数回调参数

#include <boost/lambda/lambda.hpp> 
    #include <boost/lambda/bind.hpp> 
    #include <boost/function.hpp> 
    #include <list>                                       

    class MyListChild 
    { 
    }; 

    struct Filter 
    { 
     bool matches(MyListChild& child){ return true;} 
    }; 


    class MyList 
    { 
     public: 
     typedef boost::function<bool(MyListChild&)> FilterCallback; 
     // A common function that using any callback 
     void filterChild(FilterCallback filter, std::list<MyListChild> list) 
     { 
      //... loop in children element 
      MyListChild child; 
      //filtering the child 
      if(filter(child)) list.push_back(child); 
     } 
     // A specific function that using a custom filter 
     void filterChild(const Filter& filter, std::list<MyListChild> list) 
     { 
      using namespace boost::lambda; 
      //Trying to use the common function above 
      filterChild(bind(&Filter::matches, filter, _1), list); //This will not compile. 
     } 
    }; 
    int main() 
    { 

     return 0; 
    } 

,编译器错误是:

|| /usr/include/boost/lambda/detail/actions.hpp: In instantiation of ‘static RET boost::lambda::function_action<3, T>::apply(A1&, A2&, A3&) [with RET = bool; A1 = bool (Filter::* const)(MyListChild&); A2 = const Filter; A3 = MyListChild; T = boost::lambda::detail::unspecified]’: 
    /usr/include/boost/lambda/detail/lambda_functor_base.hpp|441 col 5| required from ‘RET boost::lambda::lambda_functor_base<boost::lambda::action<3, Act>, Args>::  call(A&, B&, C&, Env&) const [with RET = bool; A = MyListChild; B = const boost::tuples::null_type; C = const boost::tuples::null_type; Env = const boost::tuples::null_type; Act = boost::lambda::function_action<3>; Args = boost::tuples::tuple<bool (Filter::* const)(MyListChild&), const Filter, const boost::lambda::lambda_functor<boost::lambda:: placeholder<1> >, 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>]’ 
    /usr/include/boost/lambda/detail/lambda_functors.hpp|195 col 50| required from ‘typename boost::lambda::lambda_functor<Base>::inherited::sig<boost::tuples::tuple<A&> >::type boost::lambda::lambda_functor<Base>::operator()(A&) const [with A = MyListChild; T = boost::lambda::lambda_functor_base<boost::lambda::action<3, boost::lambda::  function_action<3> >, boost::tuples::tuple<bool (Filter::* const)(MyListChild&), const Filter, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, 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> >; typename boost::lambda::lambda_functor<Base>::inherited::sig<boost::tuples::tuple<A&> >::type = bool]’                  
    /usr/include/boost/function/function_template.hpp|132 col 42| required from ‘static R boost::detail::function::function_obj_invoker1<FunctionObj, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionObj = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<3, boost::lambda::   function_action<3> >, boost::tuples::tuple<bool (Filter::* const)(MyListChild&), const Filter, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, 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 = MyListChild&]’ 
    /usr/include/boost/function/function_template.hpp|934 col 38| required from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = boost::lambda::   lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<3, boost::lambda::function_action<3> >, boost::tuples::tuple<bool (Filter::* const)(MyListChild&),  const Filter, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, 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 = MyListChild&]’ 
    /usr/include/boost/function/function_template.hpp|722 col 7| required from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<3,  boost::lambda::function_action<3> >, boost::tuples::tuple<bool (Filter::* const)(MyListChild&), const Filter, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, 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 = MyListChild&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’ 
    /usr/include/boost/function/function_template.hpp|1069 col 16| required from ‘boost::function<R(T0)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<3,  boost::lambda::function_action<3> >, boost::tuples::tuple<bool (Filter::* const)(MyListChild&), const Filter, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, 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 = MyListChild&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’ 
    test.cc|33 col 59| required from here 
    /usr/include/boost/lambda/detail/actions.hpp|96 col 37| error: no matching function for call to ‘boost::lambda::function_adaptor<bool (Filter::*)(MyListChild&)>::  apply(bool (Filter::* const&)(MyListChild&), const Filter&, MyListChild&)’ 
    ||  template apply<RET>(a1, a2, a3); 
    ||         ^
    /usr/include/boost/lambda/detail/actions.hpp|96 col 37| note: candidates are: 
    /usr/include/boost/lambda/detail/function_adaptors.hpp|394 col 17| note: template<class RET, class A1> static Result boost::lambda::function_adaptor<Result (Object::* )(Arg1)>::apply(Result (Object::*)(Arg1), Object*, A1&) [with RET = RET; A1 = A1; Object = Filter; Arg1 = MyListChild&; Result = bool] 
    || static Result apply(Result (Object::*func)(Arg1), Object* o, A1& a1) { 
    ||    ^
    /usr/include/boost/lambda/detail/function_adaptors.hpp|394 col 17| note: template argument deduction/substitution failed: 
    /usr/include/boost/lambda/detail/actions.hpp|96 col 37| note: cannot convert ‘a2’ (type ‘const Filter’) to type ‘Filter*’ 
    ||  template apply<RET>(a1, a2, a3); 
    ||         ^
    /usr/include/boost/lambda/detail/function_adaptors.hpp|398 col 17| note: template<class RET, class A1> static Result boost::lambda::function_adaptor<Result (Object::* )(Arg1)>::apply(Result (Object::*)(Arg1), Object&, A1&) [with RET = RET; A1 = A1; Object = Filter; Arg1 = MyListChild&; Result = bool] 
    || static Result apply(Result (Object::*func)(Arg1), Object& o, A1& a1) { 
    ||    ^
    /usr/include/boost/lambda/detail/function_adaptors.hpp|398 col 17| note: template argument deduction/substitution failed: 
    /usr/include/boost/lambda/detail/actions.hpp|96 col 37| note: cannot convert ‘a2’ (type ‘const Filter’) to type ‘Filter&’ 
    ||  template apply<RET>(a1, a2, a3); 

什么是写一个正确的方式在我的情况下提升lambda表达式?

+0

编译器错误消息将有帮助 – alexbuisson

+0

@alexbuisson编译错误被添加。 – SunLiWei

回答

1

问题解决。只需将“const”添加到Filter :: matches即可。

struct Filter 
{ 
    bool matches(MyListChild& child) const { return true;} 
}; 
相关问题