Search code examples
c++c++11boost-lambda

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)?


Solution

  • 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 :)