[function] How do you bind two boost::function objects together?
I'm trying to convert the input argument to a function object using a small helper function object. The code below looks relatively simple, but it doesn't seem to compile. Any ideas what could be wrong? template<typename inputType, typename outputType> boost::function<outputType (int)> convert(boost::function<outputType (inputType)> &object_to_convert){ boost::function<intputType (int)> input_converter(...); // The following fails to compile boost::function<outputType (int)> converted_function (boost::lambda::bind(object_to_convert, input_converter)); return converted_function }
Jason House wrote:
I'm trying to convert the input argument to a function object using a small helper function object. The code below looks relatively simple, but it doesn't seem to compile. Any ideas what could be wrong?
template<typename inputType, typename outputType> boost::function<outputType (int)> convert(boost::function<outputType (inputType)> &object_to_convert){ boost::function<intputType (int)> input_converter(...);
// The following fails to compile boost::function<outputType (int)> converted_function (boost::lambda::bind(object_to_convert, input_converter));
return converted_function }
I realize the use of local variables in the object creation is a bad idea, but that doesn't affect the compilation. While I get buried in "instantiated from here" messages, here's the actual error message (altered to match the example) /usr/local/include/boost/lambda/detail/function_adaptors.hpp:41: error: no match for call to '(const boost::function<outputType()(inputType),std::allocator<void> >) (const boost::function<outputType()(int), std::allocator<void> >&)'
On Sep 30, 2006, at 1:01 PM, Jason House wrote:
I'm trying to convert the input argument to a function object using a small helper function object. The code below looks relatively simple, but it doesn't seem to compile. Any ideas what could be wrong?
template<typename inputType, typename outputType> boost::function<outputType (int)> convert(boost::function<outputType (inputType)> &object_to_convert){ boost::function<intputType (int)> input_converter(...);
// The following fails to compile boost::function<outputType (int)> converted_function (boost::lambda::bind(object_to_convert, input_converter));
The bind expression is going to result in a function object that calls object_to_convert(input_converter). What you really want is a function object that takes one parameter ("arg1") and call object_to_object with the result of input_converter(arg1). To do so, you will need a nested bind expression that will look something like this: boost::function<outputType (int)> converted_function (boost::lambda::bind(object_to_convert, boost::lambda::bind (input_converter, arg1))); Cheers, Doug
Douglas Gregor wrote:
On Sep 30, 2006, at 1:01 PM, Jason House wrote:
I'm trying to convert the input argument to a function object using a small helper function object. The code below looks relatively simple, but it doesn't seem to compile. Any ideas what could be wrong?
template<typename inputType, typename outputType> boost::function<outputType (int)> convert(boost::function<outputType (inputType)> &object_to_convert){ boost::function<intputType (int)> input_converter(...);
// The following fails to compile boost::function<outputType (int)> converted_function (boost::lambda::bind(object_to_convert, input_converter));
The bind expression is going to result in a function object that calls object_to_convert(input_converter). What you really want is a function object that takes one parameter ("arg1") and call object_to_object with the result of input_converter(arg1). To do so, you will need a nested bind expression that will look something like this:
boost::function<outputType (int)> converted_function (boost::lambda::bind(object_to_convert, boost::lambda::bind (input_converter, arg1)));
Cheers, Doug
I want the result of the bound expression to have one input parameter that would be decided later. For instance: bool some_function(char x); boost::function<bool (char) some_function_object(some_function); boost::function<bool (int)> f = convert(some_function_object) std::cout << f(32); Unless I'm missing something, I don't think the proposed change allows that. I tried using arg1 and got a compile error. boost::lambda::arg1 didn't work either. Using boost::lambda::_1 instead produced an identical error to my original code.
participants (2)
-
Douglas Gregor
-
Jason House