Boost.Bind и композиция функторов
От: adalekin Россия IT Rightway
Дата: 08.06.10 19:06
Оценка:
Доброго времени суток, коллеги!

Хочется странного, а именно вычисления placeholder'ов (_1, _2 и т.д.) без композиции функторов. Проблема поднималась очень давно здесь, но решения так никто и не предложил. Ниже приводится законченный пример, который иллюстрирует ситуацию:

#include <boost/bind.hpp>
#include <boost/bind/protect.hpp>
#include <boost/function.hpp>

#include <iostream>

class f_runner
{
public:
    void run(boost::function<void ()> f, bool deferred)
    {
        std::cout << "From runner " << (deferred ? "( deferred ): " : ": ");
        
        f();
    }
};

class h
{
public:
    void handle(const std::string & a, const std::string & b)
    {
        std::cout << a << " - " << b;
    }
};

template<class T>
void wrapper(T f)
{
    f("a", "b");
}

int main(int argc, const char * const argv[])
{
    f_runner runner;

    h handler;

    // What do we really need:
    // runner.run(boost::bind(f, "arg1", "arg2"), true);

    boost::function<void (const std::string &, const std::string &)> f =\
        boost::bind(&h::handle, boost::ref(handler)
            , _1
            , _2);

    boost::function<void (const std::string &, const std::string &)> run =\
        boost::bind(&f_runner::run, boost::ref(runner)
            , boost::protect(boost::bind(f
                , _1
                , _2))    // evaluation of _1 and _2 is needed
            , true);
    
    wrapper(run);
    
    return 0;
}


В выделенном фрагменте используется boost::protect, чтобы предотвратить композицию и для метода f_runner::run. Однако boost::protect предотвращает вычисление placeholder'ов. Есть ли какие-нибудь пути обхода сложившейся ситуации?
boost bind composition композиция функтор functor
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.