|
Boost Users : |
Subject: Re: [Boost-users] [Bind] How do you write it?
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-03-26 23:43:13
On Tue, Mar 23, 2010 at 4:29 AM, Robert Jones <robertgbjones_at_[hidden]> wrote:
> Hi All
>
> Ok, now I've found out what is really stopping my real code working.
>
> In this example my problem is that method has defaulted arguements, which I
> think mucks up bind's signature matching. Is there an easy way round this,
> or
> must I just add appropriate overloads of double_it to A?
>
> Thanks, Rob.
>
>
> #include <algorithm>
> #include <vector>
> #include "boost/bind.hpp"
>
> struct A
> {
> Â Â Â int double_it( int i, int j = 2 ) { return i * j; }
> } 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 ) );
> }
C++ type signatures do not see optional parameters, thus, pretend that
your function does not have a default value:
#include <algorithm>
#include <vector>
#include "boost/bind.hpp"
struct A
{
int double_it( int i, int j = 2 ) { return i * j; }
} 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, 2 ) );
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net