tags:

views:

104

answers:

1

My confuse is like this code:

 #include "stdafx.h"
#include <boost/bind.hpp>

using namespace std;

void fool(std::string s)
{
 std::cout<<s<<endl;
}

void fool2()
{
 std::cout<<"test2 called\n"<<endl;
}

void fool3(std::string s1,std::string s2)
{
 std::cout<<"test3 called\n"<<endl;
}

typedef boost::function<void(std::string)> myHandler;
void mywait(myHandler handler)
{
 handler("hello my wait");
}

int main()
{
 mywait(boost::bind(fool,_1));  //it works fine as expected.

 mywait(boost::bind(fool2));   //how it works? fool2  doesnot match the  signature of  "boost::function<void(std::string)>"

 //mywait(boost::bind(fool3,_1,_2)); //if fool2 works fine, why  this not work?
 return 0;
}

the follow link is the same question.

http://forums.opensuse.org/english/development/programming-scripting/441878-how-can-boost-bind-swallow-argument-member-function.html

i just read the article: [How the Boost Bind Library Can Improve Your C++ Programs] and the boost doc about bind

those just say it works,but i don't know why. i still confused.

sorry about my poor English.wish i explained clearly yet.

+1  A: 

One of the neat things about Boost.Bind is exactly it's ability to "massage" a function into a slightly different signature.

For example, you can make your fool3 example work by explicitly giving a value for the second parameter:

mywait(boost::bind(fool3, _1, "extra parameter"));
// or even:
mywait(boost::bind(fool3, "extra parameter", _1));

Any parameters that are passed to the function which don't get used (by a _n) are simply ignored when the function is called.

Dean Harding
i know " Boost.Bind is exactly it's ability to "massage" a function into a slightly different signature.",but i just don't know how it woks? it's so magical to me.how can i know the mechanism?thanks for your reply.
Haozes
It is a kind of magic :-) [This previous question](http://stackoverflow.com/questions/112738/how-does-boost-bind-work-behind-the-scenes-in-general) goes some way to answering your question as well...
Dean Harding