2011-11-30 72 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