How can boost :: bind not match the signature, but it works fine?

My confusion is similar to 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 following 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 Bost Bind library can improve your C ++ programs] and speeding up the bind document

they just say it works, but I don’t know why. I'm still confused.

Sorry about my poor English. I have clearly explained.

+3
source share
1 answer

One of the neat things about Boost.Bind is exactly what allows you to “massage” a function into a slightly different signature.

, fool3, :

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

, , ( _ n), .

+1

Source: https://habr.com/ru/post/1756494/


All Articles