Convert function pointer type to function type

Hi, I'm desperately seeking for doing this conversion using the boost libraries. User code often provides a function pointer, while library code often works with function types. For example (function_conversion does the 'magic' here): int foo(int); template<class T, class R = typename function_conversion<T>::type > boost::function<R> function_wrapper(T t) { return boost::function<R>(t); } Such that the user can write: function_wrapper( &foo )(1); Thus no longer needs to specify the int(int) function type to boost::function. Is it possible to construct such a 'function_conversion' type ? Any answer is greatly appreciated, Peter -- Peter Soetens -- FMTC -- <http://www.fmtc.be>

Peter Soetens wrote:
One obvious way is template<class F> boost::function<F> function_wrapper( F * pf ) { return boost::function<F>( pf ); } but a more interesting question is why do you need a function_wrapper at all. Everything that you could do with the returned function<F> (call it) is also possible with pf itself.

Quoting Peter Dimov:
Looking at the simplicity of that answer, this was clearly a boost-users question :-]. Sorry. This was a minimalistic example, removing the context. The big picture uses boost::function<F> to store partially bound objects, since this is a way to store the result of boost::bind for later use in a not templated class. I got confused by the boost::function_traits<T>, which only takes a "function type" and not a "function pointer type." I assumed one was not convertible to the other. I tried using boost::remove_pointer<F>, which didn't work and confirmed back then my assumption (confusingly, is_pointer<F> did work, but I could not remove it using type_traits...) My books write about function pointer types but do not mention 'function types'. These concepts appear to me underdocumented in standard C++ works. *Thanks a lot*. Peter -- www.fmtc.be ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.

Peter Soetens wrote:
This is probably a bug (in the compiler or in type_traits); when is_pointer<F> reports true, remove_pointer<F> should work... I don't see this case covered by the type traits tests, but they aren't very readable, so I might've missed it. In any event, it won't hurt if you send a small test case to John Maddock.

Quoting Peter Dimov <pdimov@mmltd.net>:
I can't reproduce this anymore, so the error was probably between keyboard and chair. The examples of the remove_pointer docs didn't mention it either, so I gave up too early probably. Peter ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.

Peter I do not have a boost solution. I do have one using FC++, which was considered but not included in Boost. See http://www-static.cc.gatech.edu/~yannis/fc++/ The code looks like this: #include <iostream> #include "fcpp/prelude.h" int foo(int i) { return i; } int main() { std::cout << fcpp::ptr_to_fun(&foo)(1) << std::endl; // It can also be saved as an object. fcpp::Fun1<int,int> wrap_foo = fcpp::ptr_to_fun(&foo); std::cout << wrap_foo(1) << std::endl; return 0; } where fcpp points to the installation of FC++. There exists a "boostified" version of FC++ which will do the same. I hope this helps. John Fletcher Peter Soetens wrote:
participants (4)
-
John Fletcher
-
John Maddock
-
Peter Dimov
-
Peter Soetens