Boost logo

Boost :

Subject: Re: [boost] [Bind] How do I get bind to deduce the return type of for_each?
From: Roland Bock (rbock_at_[hidden])
Date: 2010-04-17 07:25:10


Thomas Jordan wrote:
> [snip]
> Thanks for the suggestion, Roland, I am sure you could get something like this to work easily enough. However, if I understand correctly your suggestion involves defining a separate functor - myClass - to take the vocType's, and which I guess wraps the inner call to for_each. I had hoped for a more generic solution which doesn't involve the creation of new functors. I was hoping I could accomplish the objective of passing an algorithm into an algorithm jgenerically, just doing everything at point of call using just Boost.bind, the std::algorithms (wrapped) and using if necessary some boost.function functors to help type deduction. Are we saying this isn't possible?
>
Hi Thomas,

we don't say it isn't possible :-)

Here you go:

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

using namespace std;
using namespace boost;

int initValue = 0;

class myClass
{
public:
   myClass():
      mValue(initValue)
   {}

   myClass(const myClass& rhs):
      mValue(initValue++)
   {
   }

   void func()
   {
      cout << mValue << endl;
   }
private:
   int mValue;
};

typedef vector<myClass> vocType;
typedef vector<vocType> vovType;

int main()
{
   vovType vov(10, vocType(10, myClass()));

   for_each(vov.begin(), vov.end(),
         bind(for_each< vocType::iterator, function<void (myClass&)> >,
            bind<vocType::iterator>(&vocType::begin, _1),
            bind<vocType::iterator>(&vocType::end, _1),
            protect(bind(&myClass::func, _1))
            )
         );
}
// -----------------------------------------------------------

Please let me know if this is working for you.

Regards,

Roland




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk