473,573 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<flo at (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<flo at>(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 2787
On 28 mai, 14:42, Tigera <tig...@rocketm ail.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<flo at (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<flo at>(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
2917
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. /**************************************************************************/ #include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp>
0
1929
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 these DLL interoperate with managed code. Among others things, the managed code need to be able to register for notification with the boost signals....
6
3996
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
6
2597
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 find it. Perhaps one of you lot might know what the typo is: #include <iostream> #include <string> #include <boost/bind.hpp> class tracer
1
3836
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 { ... }; class ActionCategory { ...
4
2481
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()); } template<class T1, class T2>
3
6800
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 pattern. gcc-4.1.2 however complains with errors about my use of boost::bind(). Doesn't bind support this way of specifying a function and its...
3
3323
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 boost::bind), but the compiler complains, because of a type mismatch in an assignment which I think should be legal:
19
3368
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 out there on the Internet on how to design *thread-safe* *efficient* data- structures? /Nordlöw
0
7767
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7684
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8006
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7768
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8058
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6400
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5288
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3729
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1035
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.