473,421 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Boost bind confusion

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
1 2778
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: P G | last post by:
I hope this is on topic here. I have a problem compiling a simple example of the use of boost::bind. Please take a look at the program below. ...
0
by: Arnaud Debaene | last post by:
Hello all. I've got a bunch of existing, non managed, C++ DLLs that export types with, among other things, public events implemented using the boost::signals library. Now, I need to have...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
6
by: Alex Buell | last post by:
I think one of the program snippets in the Boost's Bind library is wrong. I've just been trying to find the error in this program as below, perhaps there's an typo in there somewhere but I can't...
1
by: Olaf Petzold | last post by:
Hi, at this time I try to combine Qt and boost. Unfortunately I've run into trouble: void QWidget::insertAction ( QAction * before, QAction * action ); class QAction : public QWidget { ... };...
4
by: silverburgh.meryl | last post by:
I have code which uses Boost lambda in a template like this: using namespace boost::lambda; template<class T> bool lessThanXY( T& src, T& dest ) { return (src.getY() < dest.getY()); } ...
3
by: =?iso-8859-1?B?Tm9yZGz2dw==?= | last post by:
Hey there, C++ Coders! I am learning multi-threading with boost and have come up with the following code example shown below. This example implements a test of the producer-consumer design...
3
by: Giovanni Gherdovich | last post by:
Hello, in the following code I have a pointer (to function), say p, of type double (*)(double, double, void*) and I try to fix the second argument of the function *p to a given value (using...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.