[Bind] Is this how to write it?

Hi All In this bit of code.... #include <algorithm> #include <vector> #include "boost/bind.hpp" struct A { int double_it( int i ) { return i * 2; } } a; std::vector< int > v; int main( ) { for ( std::vector< int >::iterator i = v.begin( ); i != v.end( ); ++ i ) { a.double_it( * i ); } // Write it as a for_each/bind std::for_each( v.begin(), v.end(), boost::bind( & A::double_it, boost::ref( a ), _1 ) ); } Is that last line the idiomatic way of writing the bind expression? Thanks, Rob.

On Tue, Mar 23, 2010 at 9:49 AM, Robert Jones
Hi All
In this bit of code....
#include <algorithm> #include <vector> #include "boost/bind.hpp"
struct A { int double_it( int i ) { return i * 2; } } a;
std::vector< int > v;
int main( ) { for ( std::vector< int >::iterator i = v.begin( ); i != v.end( ); ++ i ) { a.double_it( * i ); }
// Write it as a for_each/bind std::for_each( v.begin(), v.end(), boost::bind( & A::double_it, boost::ref( a ), _1 ) ); }
Is that last line the idiomatic way of writing the bind expression?
Thanks, Rob.
Your code is fine when the bound thing is a method - you could use '&a' rather than 'boost::ref(a)' (a pointer to 'a' can be used for a method call in place of a reference to 'a') However, for your 'double_it', I'd personally just write a function rather than define a struct with a method. Then, 'a' is not needed and the bind would be: std::for_each( v.begin(), v.end(), boost::bind( &double_it, _1 ) ); HTH! Stuart Dootson

On Tue, Mar 23, 2010 at 10:02 AM, Stuart Dootson
Your code is fine when the bound thing is a method - you could use '&a' rather than 'boost::ref(a)' (a pointer to 'a' can be used for a method call in place of a reference to 'a')
However, for your 'double_it', I'd personally just write a function rather than define a struct with a method. Then, 'a' is not needed and the bind would be:
std::for_each( v.begin(), v.end(), boost::bind( &double_it, _1 ) );
It does indeed, thanks Stuart. I would of course lift the method from the class in real code, but this is just for exposition. In practise, as ever, there are additional constraints. - Rob. -- ACCU - Professionalism in programming - http://www.accu.org

Just in case you interested how to accomplish it with boost::lambda
#include "boost/lambda/lambda.hpp"
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
using namespace boost;
namespace bll=boost::lambda;
int main( )
{
vector< int > v;
int i=0;
// generate vector values
generate_n(back_inserter(v), 100, ++bll::var(i));
//create the output iterator
ostream_iterator<int> oiter(cout, ",");
copy(v.begin(), v.end(), oiter);
cout << endl;
// double each value in the vector
for_each(v.begin(), v.end(), bll::_1*=2);
copy(v.begin(), v.end(), oiter);
cout << endl;
return 0;
}
Regards,
Ovanes
On Tue, Mar 23, 2010 at 10:49 AM, Robert Jones
#include <algorithm> #include <vector> #include "boost/bind.hpp"
struct A { int double_it( int i ) { return i * 2; } } a;
std::vector< int > v;
int main( ) { for ( std::vector< int >::iterator i = v.begin( ); i != v.end( ); ++ i ) { a.double_it( * i ); }
// Write it as a for_each/bind std::for_each( v.begin(), v.end(), boost::bind( & A::double_it, boost::ref( a ), _1 ) ); }

On Tue, Mar 23, 2010 at 12:22 PM, Ovanes Markarian
Just in case you interested how to accomplish it with boost::lambda
#include "boost/lambda/lambda.hpp"
[...snip...]
Thanks Ovanes - I'm sure you will understand that my real code is a bit more complicated then doubling ints! Since both bind and lambda are pretty much superceded by phoenix now, perhaps you could also supply the phoenix version? - Rob.

On Tue, Mar 23, 2010 at 2:19 PM, Robert Jones
Thanks Ovanes - I'm sure you will understand that my real code is a bit more complicated then doubling ints! Since both bind and lambda are pretty much superceded by phoenix now, perhaps you could also supply the phoenix version?
As far as I know boost::phoenix is not an officially released boost lib. It
is still part of boost::spirit library. There is no top level reference to
it. So I would not consider it for now to be a replacement for bind and
lambda. It might become, but don't rely on that. ;) Anyway here the
rewritten example:
#include

Hi Ovanes, as far as i understand is phoenix an officially released boost lib. It has been reviewed.See http://lambda-the-ultimate.org/node/3012 and http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/index.html Cheers, Kim Ovanes Markarian schrieb:
On Tue, Mar 23, 2010 at 2:19 PM, Robert Jones
mailto:robertgbjones@gmail.com> wrote: Thanks Ovanes - I'm sure you will understand that my real code is a bit more complicated then doubling ints! Since both bind and lambda are pretty much superceded by phoenix now, perhaps you could also supply the phoenix version?
As far as I know boost::phoenix is not an officially released boost lib. It is still part of boost::spirit library. There is no top level reference to it. So I would not consider it for now to be a replacement for bind and lambda. It might become, but don't rely on that. ;) Anyway here the rewritten example:
#include
#include #include #include #include <algorithm> #include <vector> #include <iterator> #include <iostream>
using namespace std; using namespace boost; using namespace boost::phoenix; using namespace boost::phoenix::arg_names; using namespace boost::phoenix::local_names;
namespace ph = boost::phoenix;
int main( ) { vector< int > v; // generate vector values int i=0; generate_n(back_inserter(v), 100, ++ph::ref(i));
//create the output iterator ostream_iterator<int> oiter(cout, ","); copy(v.begin(), v.end(), oiter); cout << endl;
// double each value in the vector for_each(v.begin(), v.end(), arg1*=2); copy(v.begin(), v.end(), oiter); cout << endl; }
Regards, Ovanes ------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Tue, Mar 23, 2010 at 5:46 PM, Kim Kuen Tang
Hi Ovanes,
as far as i understand is phoenix an officially released boost lib. It has been reviewed.See http://lambda-the-ultimate.org/node/3012 and
http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/index.html
Cheers, Kim
Kim, to be reviewed does not mean to be accepted into boost. There are many libs which have been reviewed, but never become a part of boost. Even if phoenix is going to be accepted into boost, it must become a top level lib. Currently it is not the case. After the review there might be provisions under which the library will be accepted (e.g. more documentation, or modified interface etc.). Currently I see it as implementation detail inside the spirit-framework. And therefore it might change with any release. I think this library is already stable, but it is not part of a boost top-level-libs. Regards, Ovanes

Robert Jones wrote:
Thanks Ovanes - I'm sure you will understand that my real code is a bit more complicated then doubling ints! Since both bind and lambda are pretty much superceded by phoenix now, perhaps you could also supply the phoenix version?
- Rob.
Rob -
I highly recommend moving to phoenix (despite comments made later in
this thread). The library is extremely powerful and stable and has been
conditionally accepted (
http://lists.boost.org/boost-announce/2008/10/0205.php ). The
documentation is excellent and the support is speedy. I find phoenix
constructs to be far more readable than boost.lambda resulting in
overall improved understanding of the code's intent.
Documentation can be found here:
http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/index.html
Here is your example using phoenix -------------------
#include <iostream>
#include <vector>
#include

On Tue, Mar 23, 2010 at 8:28 PM, Michael Caisse < boost@objectmodelingdesigns.com> wrote:
I highly recommend moving to phoenix (despite comments made later in this thread). The library is extremely powerful and stable and has been conditionally accepted ( < http://lists.boost.org/boost-announce/2008/10/0205.php> ). The documentation is excellent and the support is speedy. I find phoenix constructs to be far more readable than boost.lambda resulting in overall improved understanding of the code's intent.
Documentation can be found here: < http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/index.html
Thanks Michael - I would indeed migrate to Phoenix by choice, however we're tied to a rather older version of Boost (1.37) at them moment, when, I believe(?), Phoenix was less complete/polished/stable. Cheers, Rob.
participants (5)
-
Kim Kuen Tang
-
Michael Caisse
-
Ovanes Markarian
-
Robert Jones
-
Stuart Dootson