C ++ for_each calling a vector of callback functions and passing each argument

I'm pretty green when it comes to C ++ 0x, lambda, etc. Hope you guys can help me with this little problem.

I want to save a bunch of callbacks in a vector, and then use for_each to call them when the time is right. I want callback functions to be able to take arguments. Here is my code right now. The problem is the void B :: do_another_callbacks (std :: string &)

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <vector>
#include <iostream>
#include <algorithm>



class A {
public:
    void print(std::string &s) {
        std::cout << s.c_str() << std::endl;
    }
};

typedef boost::function<void(std::string&)> another_callback;
typedef boost::function<void()> callback;

typedef std::vector<callback> callback_vector;
typedef std::vector<another_callback> another_callback_vector;

class B {
public:
    void add_callback(callback cb) {
        m_cb.push_back(cb);
    }

    void add_another_callback(another_callback acb) {
        m_acb.push_back(acb);
    }

    void do_callbacks() {
        for_each(m_cb.begin(), m_cb.end(), this);
    }

    void do_another_callbacks(std::string &s) {
        std::tr1::function<void(another_callback , std::string &)> my_func = [] (another_callback acb, std::string &s) { acb(s); }
        for_each(m_acb.begin(), m_acb.end(), my_func(_1, s));
    }

    void operator() (callback cb) { cb(); }

private:
    callback_vector m_cb;
    another_callback_vector m_acb;
};

void main() {
    A a;
    B b;
    std::string s("message");
    std::string q("question");
    b.add_callback(boost::bind(&A::print, &a, s));
    b.add_callback(boost::bind(&A::print, &a, q));
    b.add_another_callback(boost::bind(&A::print, &a, _1));
    b.do_callbacks();
    b.do_another_callbacks(s);
    b.do_another_callbacks(q);
}

I thought I could do something like this ...

void do_another_callbacks(std::string &s) {

    for_each(m_acb.begin(), m_acb.end(), [&s](another_callback acb) {
        acb(s);
    });
}

But this does not compile in MSVC2010

+3
source share
1 answer

, my_func(_1,s) . std::bind ( boost::bind) .

, , , - do_callbacks:

void do_callbacks() {
    for_each(m_cb.begin(), m_cb.end(), this);
}

this B*, . result_type typedef operator(), std::ref(*this). MSVC10:

#include <functional>
#include <vector>
#include <iostream>
#include <algorithm>



class A {
public:
    void print(std::string &s) {
        std::cout << s.c_str() << std::endl;
    }
};

typedef std::function<void(std::string&)> another_callback;
typedef std::function<void()> callback;

typedef std::vector<callback> callback_vector;
typedef std::vector<another_callback> another_callback_vector;

class B {
public:
    void add_callback(callback cb) {
        m_cb.push_back(cb);
    }

    void add_another_callback(another_callback acb) {
        m_acb.push_back(acb);
    }

    void do_callbacks() {
        std::for_each(m_cb.begin(), m_cb.end(), std::ref(*this));
    }

    void do_another_callbacks(std::string &s) {

        std::for_each(m_acb.begin(), m_acb.end(), [&s](another_callback acb) {
                acb(s);
            });
    }

    typedef void result_type;
    void operator() (callback cb) { cb(); }

private:
    callback_vector m_cb;
    another_callback_vector m_acb;
};

int main() {
    A a;
    B b;
    std::string s("message");
    std::string q("question");
    b.add_callback(std::bind(&A::print, &a, s));
    b.add_callback(std::bind(&A::print, &a, q));
    b.add_another_callback(std::bind(&A::print, &a, std::placeholders::_1));
    b.do_callbacks();
    b.do_another_callbacks(s);
    b.do_another_callbacks(q);
}
+5

Source: https://habr.com/ru/post/1754846/


All Articles