title: C++ for_each calling a vector of callback functions and passing each one an argument
body: <p>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.</p>
  
  <p>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 &amp;)</p>
  
  <pre><code>#include &lt;boost/bind.hpp&gt;
  #include &lt;boost/function.hpp&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  
  
  
  class A {
  public:
      void print(std::string &amp;s) {
          std::cout &lt;&lt; s.c_str() &lt;&lt; std::endl;
      }
  };
  
  typedef boost::function&lt;void(std::string&amp;)&gt; another_callback;
  typedef boost::function&lt;void()&gt; callback;
  
  typedef std::vector&lt;callback&gt; callback_vector;
  typedef std::vector&lt;another_callback&gt; 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 &amp;s) {
          std::tr1::function&lt;void(another_callback , std::string &amp;)&gt; my_func = [] (another_callback acb, std::string &amp;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(&amp;A::print, &amp;a, s));
      b.add_callback(boost::bind(&amp;A::print, &amp;a, q));
      b.add_another_callback(boost::bind(&amp;A::print, &amp;a, _1));
      b.do_callbacks();
      b.do_another_callbacks(s);
      b.do_another_callbacks(q);
  }
  </code></pre>
  
  <p>I thought I might be able to do something like this... </p>
  
  <pre><code>void do_another_callbacks(std::string &amp;s) {
  
      for_each(m_acb.begin(), m_acb.end(), [&amp;s](another_callback acb) {
          acb(s);
      });
  }
  </code></pre>
  
  <p>But that doesn't compile in MSVC2010</p>