boost::bind 与提供的签名不匹配但工作正常怎么办?
我的困惑就像这段代码:
#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;
}
以下链接是同一个问题。
我刚刚读了这篇文章: [Boost Bind 库如何改进您的 C++ 程序] 关于绑定的增强文档
只是说它有效,但我不知道为什么。我还是很困惑。
抱歉我的英语不好。希望我能解释清楚。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Boost.Bind 的优点之一就是它能够将函数“按摩”成稍微不同的签名。
例如,您可以通过显式为第二个参数指定值来使您的
fool3
示例正常工作:传递给函数但不使用的任何参数(通过 _n< /i>) 在调用该函数时会被忽略。
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:Any parameters that are passed to the function which don't get used (by a _n) are simply ignored when the function is called.