Discussion:
Using std::bind2nd with class member functions which accept const reference as parameter
(too old to reply)
Vladimir Grigoriev
2009-12-07 13:40:25 UTC
Permalink
Let assume there is a class

struct Point
{
explicit Point( int i = 0, int j = 0 ) : x( i ), y( j ) {}
bool equal_to( Point rhs ) const
{
return ( ( x == rhs.x ) && ( y == rhs.y ) );
}
int x, y;
};

std::vector<Point> v;

for ( int i = 0; i < 10; ++i )
{
v.push_back( Point( i, i ) );
}

Then it may be write down
std::vector<Point>::iterator it;

it = std::find_if( v.begin(), v.end(), std::bind2nd(
std::const_mem_fun1_ref_t<bool, Point, Point>( &Point::equal_to ), Point( 5,
5 ) ) );

The code above works.

However if the member function equal_to will be changed as

bool equal_to( const Point &rhs ) const;
then how can it be called the find_if() for this member function?

Vladimir Grigoriev
Igor Tandetnik
2009-12-07 14:22:14 UTC
Permalink
Post by Vladimir Grigoriev
it = std::find_if( v.begin(), v.end(), std::bind2nd(
std::const_mem_fun1_ref_t<bool, Point, Point>( &Point::equal_to ), Point( 5,
5 ) ) );
The code above works.
However if the member function equal_to will be changed as
bool equal_to( const Point &rhs ) const;
then how can it be called the find_if() for this member function?
Basically, it can't. It's a known problem with the standard that bind* family can't handle reference parameters. C++0x deals with it by a) allowing references to references (T& & simply collaspes to T&; I think this was added in C++03); and b) providing the new, very flexible bind() function that can take pretty much anything, modeled after boost::bind:

http://www.boost.org/doc/libs/1_41_0/libs/bind/bind.html

and deprecating bind2nd et al.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Vladimir Grigoriev
2009-12-07 14:35:13 UTC
Permalink
Thanks, Igor for your full answer.

Vladimir Grigoriev
Post by Vladimir Grigoriev
it = std::find_if( v.begin(), v.end(), std::bind2nd(
std::const_mem_fun1_ref_t<bool, Point, Point>( &Point::equal_to ), Point( 5,
5 ) ) );
The code above works.
However if the member function equal_to will be changed as
bool equal_to( const Point &rhs ) const;
then how can it be called the find_if() for this member function?
Basically, it can't. It's a known problem with the standard that bind*
family can't handle reference parameters. C++0x deals with it by a) allowing
references to references (T& & simply collaspes to T&; I think this was
added in C++03); and b) providing the new, very flexible bind() function
that can take pretty much anything, modeled after boost::bind:

http://www.boost.org/doc/libs/1_41_0/libs/bind/bind.html

and deprecating bind2nd et al.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925
Loading...