By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

Boost bind confusion

P: n/a
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.

May 28 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 28 mai, 14:42, Tigera <tig...@rocketmail.comwrote:
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
May 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.