boost :: bind不同的函数有不同的返回类型

时间:2011-11-30 10:20:24

标签: c++ function boost bind

我需要某种回调为boost::function<void(void)>,回调由boost::bind分配,代码如下:

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的包装是一种方式,但对于我们的项目,没有必要。

1 个答案:

答案 0 :(得分:0)

此代码无法完全编译。您不能将float绑定到期望字符串参数的函数。如果我尝试使用boost 1.34.1&amp;编译代码,我得到的错误消息示例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