Boost bind confusion

T

Tigera

Greetings,

I am trying to learn to use the Boost library, and I've run into a
frustrating problem with the line marked here:

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

using std::cout;
using std::endl;

class A { };

class B { };

class C { };

class D { };


class Visitor
{
public:
boost::function<float (A first)> foo;
};


class VisitorAB
{
public:
float operator()(A first, B second) { return 0.0; }
};


class VisitorAC
{
public:
float operator()(A first, B second) { return 1.0; }
float operator()(A first, C second) { return 2.0; }
float bar(A first, C second) { return 3.0; }
};


int main()
{
VisitorAB vab;
VisitorAC vac;
Visitor v;

A a;
B b;
C c;

v.foo = boost::bind2nd(vac, c); //Gives me a compile error

return 0;
}

I've also tried these:
v.foo = boost::bind(vac, _1, c);
v.foo = boost::bind<float (a)>(vac, _1, c);
v.foo = boost::bind( boost::mem_fun_ref(&VisitorAC::bar), c);

I finally found after about three hours that if I used v.foo =
boost::bind<float>(vac, _1, c) that I could cajole the program into
compiling.
I'm confused on the use of bind. What could I have done to make this
work with boost::bind2nd? Why is it that boost::bind only works when
you specify a template parameter?
Your answers are appreciated.
 
E

Eric.Malenfant

Greetings,

I am trying to learn to use the Boost library, and I've run into a
frustrating problem with the line marked here:
[snip]

int main()
{
VisitorAB vab;
VisitorAC vac;
Visitor v;

A a;
B b;
C c;

v.foo = boost::bind2nd(vac, c); //Gives me a compile error

As far as I know, there is no "boost::bind2nd", only boost::bind.
There is a std::bind2nd, tough.
I've also tried these:
v.foo = boost::bind(vac, _1, c);
v.foo = boost::bind<float (a)>(vac, _1, c);
v.foo = boost::bind( boost::mem_fun_ref(&VisitorAC::bar), c);

I finally found after about three hours that if I used v.foo =
boost::bind<float>(vac, _1, c) that I could cajole the program into
compiling.
[snip]

Why is it that boost::bind only works when
you specify a template parameter?

Boost.Bind documentation has a section on just this:
http://www.boost.org/libs/bind/bind.html#Q_forms

HTH,

Éric
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top