473,703 Members | 2,907 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 2792
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
2936
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
1939
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. I've tried different approaches (trying to register a pinned delegate with the boost::signal, etc...),
6
4024
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
6
2603
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
3846
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
2487
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
6812
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 arguments or should I solve it in another way?
3
3336
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
3388
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
8743
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8659
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9239
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9098
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8993
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6585
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5920
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4417
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.