Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 930329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:18:59+00:00 2026-05-15T20:18:59+00:00

I’m fairly green when it comes to c++0x, lambda, and such so I hope

  • 0

I’m fairly green when it comes to c++0x, lambda, and such so I hope you guys can help me out with this little problem.

I want to store a bunch of callbacks in a vector and then use for_each to call them when the time is right. I want the callback functions to be able to accept arguments. Here’s my code right now. The trouble is in 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 might be able to 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 that doesn’t compile in MSVC2010

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:19:00+00:00Added an answer on May 15, 2026 at 8:19 pm

    The problem with the long example is that my_func(_1,s) is evaluated right there and then. You need to use std::bind (or boost::bind) to invoke the function on each element in the range.

    The alternative code that you posted does indeed work, but the whole example fails to compile because of the code in do_callbacks:

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

    this is of type B*, which is not callable. If you define a result_type typedef to match the return type of operator() then you could use std::ref(*this) instead. The following code compiles and runs under 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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.