2011-11-30 70 views
0

我需要某种形式的回调为boost::function<void(void)>,并且回调被boost::bind分配,这样的代码:的boost ::绑定不同的功能有不同的返回类型

bool func1(int i); 
double func2(std::string str); 

typedef boost::function<void(void)> callback; 

callback cb1 = boost::bind(func1, 1); 
callback cb2 = boost::bind(func2, 1.0f); 

cb1(); //what happen here? 
cb2(); //how about this? 

该代码可以通过VC8编译,并在跑步过程中没问题。 我的提升是1.34,这是一些类似的提升错误1.34
或者这种代码可能会导致堆栈崩溃。

如果是这样,应该怎么做才能解决这个问题,做一个func1和func2的包装是一种方式,但对于我们的项目来说,没有必要。

+2

你为什么使用这样一个旧版本的Boost? –

+0

你应该使用一个足够强大的Boost版本,它的'function'实现可以处理这些。 –

+0

如果更高版本解决了这个问题,我可能会转向更新的版本。 – user1073072

回答

0

这段代码没有办法干净地编译。对于需要字符串参数的函数,您不能绑定float。错误spewage的例子,我得到,如果我试着和升压编译代码1.34.1 & G ++ 4.1.2:

include/boost-1_34_1/boost/bind.hpp: In member function 'R 
boost::_bi::list1<A1>::operator()(boost::_bi::type<R>, F&, A&, long 
int) [with R = double, F = double (*)(std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >), A = 
boost::_bi::list0, A1 = boost::_bi::value<float>]': 

include/boost-1_34_1/boost/bind/bind_template.hpp:20: instantiated 
from 'typename boost::_bi::result_traits<R, F>::type 
boost::_bi::bind_t<R, F, L>::operator()() [with R = double, F = double 
(*)(std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >), L = 
boost::_bi::list1<boost::_bi::value<float> >]' 

boost-1_34_1/boost/function/function_template.hpp:158: instantiated 
from 'static void 
boost::detail::function::void_function_obj_invoker0<FunctionObj, 
R>::invoke(boost::detail::function::function_buffer&) [with 
FunctionObj = boost::_bi::bind_t<double, double 
(*)(std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >), boost::_bi::list1<boost::_bi::value<float> > 
>, R = void]' 

include/boost-1_34_1/boost/function/function_template.hpp:787: 
instantiated from 'void boost::function0<R, 
Allocator>::assign_to(const Functor&) [with Functor = 
boost::_bi::bind_t<double, double (*)(std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >), 
boost::_bi::list1<boost::_bi::value<float> > >, R = void, Allocator = 
std::allocator<void>]' 

include/boost-1_34_1/boost/function/function_template.hpp:624: 
instantiated from 'boost::function0<R, Allocator>::function0(Functor, 
typename 
boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, 
int>::type) [with Functor = boost::_bi::bind_t<double, double 
(*)(std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >), boost::_bi::list1<boost::_bi::value<float> > 
>, R = void, Allocator = std::allocator<void>]' 

include/boost-1_34_1/boost/function/function_template.hpp:886: 
instantiated from 'boost::function<R()(), 
Allocator>::function(Functor, typename 
boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, 
int>::type) [with Functor = boost::_bi::bind_t<double, double 
(*)(std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >), boost::_bi::list1<boost::_bi::value<float> > 
>, R = void, Allocator = std::allocator<void>]' 

testbind.cpp:18: instantiated from here 
include/boost-1_34_1/boost/bind.hpp:221: error: conversion from 
'float' to non-scalar type 'std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >' requested 
+0

感谢您的回复,vc8可以清晰地编译代码,不会出现任何遗漏和错误。在使用上面的代码测试应用程序之后,我遇到了很多问题,应用程序有一些奇怪的行为。所以我决定修改代码,不要这样写代码。谢谢。 – user1073072