Boost logo

Boost :

Subject: Re: [boost] [Bind] How do I get bind to deduce the return type of for_each?
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-04-20 01:15:45


On Mon, Apr 19, 2010 at 1:04 PM, Thomas Jordan
<thomasjordan_at_[hidden]> wrote:
> 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?
>>
>
>
> Roland Bock replied:
>
> ************************************
>
> 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
>
>
>
> **************************************************************
>
> Thanks a lot, Roland, this looks good. I felt there must be some way to do it, but it was just beyond my grasp.

You should use Boost.Phoenix, it can do what Bind and Lambda and
others can, plus more, including the above with no issues.


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