Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of boost lambda

Consider the following piece of C++0x code:

a_signal.connect([](int i) {
  if(boost::any_cast<std::string>(_buffer[i]) == "foo")
  {
    base_class<>* an_object = new derived_class();
    an_object->a_method(_buffer[i]);
  }});

How would it correctly look in Boost Lambda (since this C++0x feature can't be used in GCC 4.4 yet)?

like image 233
Karl von Moor Avatar asked May 13 '10 11:05

Karl von Moor


1 Answers

I think this should work:

a_signal.connect(if_then(
                  bind((std::string(*)(any&))&any_cast, var(_buffer)[_1]) == "foo",
                   bind(&base_class<>::a_method, 
                    ll_static_cast< base_class<>* >(
                     new_ptr<derived_class>()
                    ), 
                    var(_buffer)[_1]
                   )
                 )
);

bind, if_then, ll_static_cast, new_ptr, _1, var (and, i think ref too) are members of boost::lambda.

But honestly, i would refuse to work with such code, personally :)

like image 103
Johannes Schaub - litb Avatar answered Sep 19 '22 19:09

Johannes Schaub - litb