c++ - How can boost::bind does not match the signature provided but works fine? -
my confuse 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 wait"); } int main() { mywait(boost::bind(fool,_1)); //it works fine expected. mywait(boost::bind(fool2)); //how works? fool2 doesnot match signature of "boost::function<void(std::string)>" //mywait(boost::bind(fool3,_1,_2)); //if fool2 works fine, why not work? return 0; }
the follow link same question.
i read article: [how boost bind library can improve c++ programs] , boost doc bind
those works,but don't know why. still confused.
sorry poor english.wish explained yet.
one of neat things boost.bind it's ability "massage" function different signature.
for example, can make fool3
example work explicitly giving value second parameter:
mywait(boost::bind(fool3, _1, "extra parameter")); // or even: mywait(boost::bind(fool3, "extra parameter", _1));
any parameters passed function don't used (by _n) ignored when function called.
Comments
Post a Comment