请教boost::bind中使用的一个问题.

s_51563946 2010-09-05 09:44:51
精简后的代码如下:

#include <boost/bind.hpp>
#include <boost/system/error_code.hpp>
#include <string>

template<typename Handler1, typename Handler2>
void two_handler_test_func(const char* p1, const char* p2, Handler1 handler1, Handler2 handler2)
{
handler1(boost::system::error_code());
handler2(boost::system::error_code());
}

class MyTestClass
{
public:
template<typename Handler>
void test_func(const std::string& p1, const std::string& p2, Handler handler)
{
two_handler_test_func(p1.c_str(), p2.c_str(),
boost::bind(&MyTestClass::handle_test_func1<Handler>, this, _1, handler),
boost::bind(&MyTestClass::handle_test_func2, this, _1, 2));
}

private:
template<typename Handler>
void handle_test_func1(const boost::system::error_code& ec, Handler handler)
{
handler(ec);
}

void handle_test_func2(const boost::system::error_code& ec, int b)
{}

};

void command_listen_finished(const boost::system::error_code& ec, int i)
{}

void command_listen_finished2(const boost::system::error_code& ec)
{}

class TestCallback
{
public:
TestCallback(int i)
:m_i(i)
{}

void operator()(const boost::system::error_code& ec)
{}
private:
int m_i;
};

int main()
{
MyTestClass testClass;
testClass.test_func("abc", "def", boost::bind(command_listen_finished, _1, 1));
//testClass.test_func("abc", "def", command_listen_finished2);
//testClass.test_func("abc", "def", TestCallback(1));
return 0;
}


最后三个函数,testClass.test_func:
第一个无法编译通过,第二个和第三个都可以直接编译通过.
按道理来讲,第一个其实应该是boost::bind构造了一个和TestCallback一样的函数对象.
请教这里面是哪里写错了?

我使用的是VC9,版本编号是:
Microsoft Visual Studio 2008
Version 9.0.30729.1 SP
Microsoft .NET Framework
Version 3.5 SP1

Installed Edition: Enterprise

Microsoft Visual C++ 2008 91899-153-0000007-60183
...全文
318 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
s_51563946 2010-09-06
  • 打赏
  • 举报
回复
好吧,正解在这里:
http://www.codeguru.com/forum/showthread.php?p=1966066
第一次听说还有boost::protect这个东西,膜拜一下牛人……
taodm 2010-09-06
  • 打赏
  • 举报
回复
bind不是万能的,据说根据C++标准,在函数return值为void时,且又满足一定条件时(抱歉,记不住那么多,自己翻《C++标准库扩展权威指南》吧),会类型推导失败。
ayw215 2010-09-06
  • 打赏
  • 举报
回复
bind返回的是一个函数对象,而不是XX类型,所以不能实例化::handle_test_func1<Handler>,
job82824 2010-09-06
  • 打赏
  • 举报
回复
这个呀,慢慢改吧。

比如这个'void' to 'boost::_bi::bind_t<R,F,L>'类型不匹配,一个是无返回型的,一个是有返回性的
s_51563946 2010-09-06
  • 打赏
  • 举报
回复
模板错误很大一堆的:
main.cpp
1>f:\libraries\boost_1_44_0\boost\bind\bind.hpp(392) : error C2664: 'R boost::_mfi::mf2<R,T,A1,A2>::operator ()<MyTestClass>(const U &,A1,A2) const' : cannot convert parameter 3 from 'void' to 'boost::_bi::bind_t<R,F,L>'
1> with
1> [
1> R=void,
1> T=MyTestClass,
1> A1=const boost::system::error_code &,
1> A2=boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>,
1> U=MyTestClass *
1> ]
1> and
1> [
1> R=void,
1> F=void (__cdecl *)(const boost::system::error_code &,int),
1> L=boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>
1> ]
1> Expressions of type void cannot be converted to other types
1> f:\libraries\boost_1_44_0\boost\bind\bind_template.hpp(47) : see reference to function template instantiation 'void boost::_bi::list3<A1,A2,A3>::operator ()<F,boost::_bi::list1<const boost::system::error_code &>>(boost::_bi::type<T>,F &,A &,int)' being compiled
1> with
1> [
1> A1=boost::_bi::value<MyTestClass *>,
1> A2=boost::arg<1>,
1> A3=boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>,
1> F=boost::_mfi::mf2<void,MyTestClass,const boost::system::error_code &,boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>>,
1> T=void,
1> A=boost::_bi::list1<const boost::system::error_code &>
1> ]
1> c:\documents and settings\watsonsong\my documents\visual studio 2008\projects\testbind\testbind\main.cpp(8) : see reference to function template instantiation 'void boost::_bi::bind_t<R,F,L>::operator ()<boost::system::error_code>(const A1 &)' being compiled
1> with
1> [
1> R=void,
1> F=boost::_mfi::mf2<void,MyTestClass,const boost::system::error_code &,boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>>,
1> L=boost::_bi::list3<boost::_bi::value<MyTestClass *>,boost::arg<1>,boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>>,
1> A1=boost::system::error_code
1> ]
1> c:\documents and settings\watsonsong\my documents\visual studio 2008\projects\testbind\testbind\main.cpp(20) : see reference to function template instantiation 'void two_handler_test_func<boost::_bi::bind_t<R,F,L>,boost::_bi::bind_t<R,boost::_mfi::mf2<R,T,A1,A2>,boost::_bi::list3<boost::_bi::value<MyTestClass>,boost::arg<I>,A3>>>(const char *,const char *,Handler1,Handler2)' being compiled
1> with
1> [
1> R=void,
1> F=boost::_mfi::mf2<void,MyTestClass,const boost::system::error_code &,boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>>,
1> L=boost::_bi::list3<boost::_bi::value<MyTestClass *>,boost::arg<1>,boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>>,
1> T=MyTestClass,
1> A1=const boost::system::error_code &,
1> A2=int,
1> I=1,
1> A3=boost::_bi::value<int>,
1> Handler1=boost::_bi::bind_t<void,boost::_mfi::mf2<void,MyTestClass,const boost::system::error_code &,boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>>,boost::_bi::list3<boost::_bi::value<MyTestClass *>,boost::arg<1>,boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>>>,
1> Handler2=boost::_bi::bind_t<void,boost::_mfi::mf2<void,MyTestClass,const boost::system::error_code &,int>,boost::_bi::list3<boost::_bi::value<MyTestClass *>,boost::arg<1>,boost::_bi::value<int>>>
1> ]
1> c:\documents and settings\watsonsong\my documents\visual studio 2008\projects\testbind\testbind\main.cpp(57) : see reference to function template instantiation 'void MyTestClass::test_func<boost::_bi::bind_t<R,F,L>>(const std::string &,const std::string &,Handler)' being compiled
1> with
1> [
1> R=void,
1> F=void (__cdecl *)(const boost::system::error_code &,int),
1> L=boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>,
1> Handler=boost::_bi::bind_t<void,void (__cdecl *)(const boost::system::error_code &,int),boost::_bi::list2<boost::arg<1>,boost::_bi::value<int>>>
1> ]
job82824 2010-09-06
  • 打赏
  • 举报
回复
代码太多--直接贴出错信息吧。
taodm 2010-09-06
  • 打赏
  • 举报
回复
testClass.test_func("abc", "def", function<int (const boost::system::error_code&)>(boost::bind(command_listen_finished, _1, 1)));

《C++标准库扩展权威指南》英文电子版可以google。
s_51563946 2010-09-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 taodm 的回复:]
bind不是万能的,据说根据C++标准,在函数return值为void时,且又满足一定条件时(抱歉,记不住那么多,自己翻《C++标准库扩展权威指南》吧),会类型推导失败。
[/Quote]
另外,跪求《C++标准库扩展权威指南》电子版...
s_51563946 2010-09-05
  • 打赏
  • 举报
回复
顺便说一下,我使用的是boost 1.4.3

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧