question on boost::bind

O

osama178

Hi,

I am just starting to read about and learn boost. I wrote this
program, but it won't compile:

-------------
#include <iostream>
#include <functional>
#include <boost/bind.hpp>

class item
{
public:
item( int id) : id_(id) {}

int id() const {return id_; }

private:
int id_;

};

class comparator
{
public:
bool operator () (const item& e)
{
return boost::bind( std::less<int>(), 10,
boost::bind(&item::id, _1));
}
};

---------------------

the error I get is:
cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'bool'

my understanding is, the call:

return boost::bind( std::less<int>(), 10,
boost::bind(&item::id, _1));

would be transferred to a call to std::less, with 10 as the first
parameter, and e.id() as the second paramter. And std::less returns a
boolean.

Any help is appreciated.
 
K

Kram

Hi,

I am just starting to read about and learn boost. I wrote this
program, but it won't compile:

-------------
#include <iostream>
#include <functional>
#include <boost/bind.hpp>

class item
{
public:
        item( int id) : id_(id) {}

        int id() const {return id_; }

private:
         int id_;

};

class comparator
{
public:
        bool operator () (const item& e)
        {
                return boost::bind( std::less<int>(), 10,
                        boost::bind(&item::id, _1));
        }

};

---------------------

the error I get is:
cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'bool'

my understanding is, the call:

return boost::bind( std::less<int>(), 10,
                        boost::bind(&item::id, _1));

would be transferred to a call to std::less, with 10 as the first
parameter, and e.id() as the second paramter. And std::less returns a
boolean.

Any help is appreciated.

Non-standard library questions should probably be directed toward that
libraries group, try boost users.
 
O

osama178

bind() returns a function object, that is, an object of a type that has
an operator() that you can call. So you have to call it:

        boost::bind(std::less<int>(), 10,
                boost::bind(&item::id, _1))(e)

Thanks Pete. That did fix it, and I could see now why we need to pass
in an instance of e.

But in that case (bind returning a function object), doesn't the inner
bind: boost::bind(&item::id, _1) also return a function object with
operator(), which we have to call like this:


boost::bind(std::less<int>(), 10,
boost::bind(&item::id, _1) () )(e)

Thanks.
 
O

osama178

bind() returns a function object, that is, an object of a type that has
an operator() that you can call. So you have to call it:
boost::bind(std::less<int>(), 10,
boost::bind(&item::id, _1))(e)

Thanks Pete. That did fix it.

But in that case (bind returning a function object), doesn't the inner
bind: boost::bind(&item::id, _1) also return a function object with
operator(), which we have to call like this:

boost::bind(std::less<int>(), 10,
boost::bind(&item::id, _1) () )(e)

Thanks.
 
B

Bertrand

Thanks Pete. That did fix it.

But in that case (bind returning a function object), doesn't the inner
bind: boost::bind(&item::id, _1) also return a function object with
operator(), which we have to call like this:

boost::bind(std::less<int>(), 10,
boost::bind(&item::id, _1) () )(e)

Thanks.
No, you can compose the two functions objects, as in:
f: int,int -> bool,
g: item -> int,
f( 10, g ): o -> bool
all arguments of f are bound (to an int value and the output of g
respectively).

2 comments:
- why do you define a functor (quoted in OP), the point of bind (imho)
is to avoid this:
Iterator it = std::find_if( c.begin(), c.end(),
boost::bind( std::less<int>(), 10, boost::bind( &item::id, _1 )));
- not that the functor can also be written like this:
10 < boost::bind( &item::id, _1 )
which is even more concise/clear. so in above example:
Iterator it = std::find_if( c.begin(), c.end(),
10 < boost::bind( &item::id, _1 ))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top