Boost Bind in C++


Boost.Bind

bind use placehoders concept. bind can have nine argument placeholders.
#include <boost/bind.hpp>

Why use this
 compare to std::bind1st or std::bind2nd can support upto 2 parameters.
 you don't need to use different syntex for pointer or noraml object like std::mem_fun, std::mem_fun_ref.


function binding
void testthree(int i1,int i2, int i3) { std::cout<<i1<<"\t"<<i2<<"\t"<<i3<<"\n"; } int i1=1,i2=2; boost::bind(&testthree,_2,_1,_2)(i1,i2);

result would be : 2 1 2

class member function binding
class test { public: void testmemfun(int i, int j) { std::cout<<i<<"\t"<<j<<"\n"; } }; boost::bind(&test::testmemfun,test(),_1,_2)(param1,param2);

passing in algorithem this pointer or object passed as first parameter.

for_each(vectest.begin(),vectest.end(),boost::bind(&test::testmemfun,_1,param1,param2));
//for pointer container
for_each(vectest.begin(),vectest.end(),std::mem_fun(&test::testmemfun));
//for object container
for_each(vectest.begin(),vectest.end(),boost::bind(&test::testmemfun));  

No comments:

Post a Comment

would you like it. :)