boost::bind 与提供的签名不匹配但工作正常怎么办?

发布于 2024-09-11 21:09:06 字数 1218 浏览 3 评论 0原文

我的困惑就像这段代码:

 #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;
}

以下链接是同一个问题。

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

我刚刚读了这篇文章: [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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2024-09-18 21:09:06

Boost.Bind 的优点之一就是它能够将函数“按摩”成稍微不同的签名。

例如,您可以通过显式为第二个参数指定值来使您的 fool3 示例正常工作:

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

传递给函数但不使用的任何参数(通过 _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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文