[fusion]Is it possible to wrap a function template into a function object?
If there is a function template: template<typename T> T f(T t); then f is not a type and cannot be passed to fusion::for_each() as the second argument. But I can't write fusion::for_each(someSequence,&f<SomeType>) either since elements of someSequence has different types. While I can write f as a functor fo struct fo{ template<typename T> T operator()(T t); }; and call fusion::for_each(someSequence,fo()), sometimes I need to use function templates in libraries ( say, I have a fusion::vector whose elements are ranges ) and the only solution seems to be writing a lot of functors to wrap the function templates. I tried to find a wrapper class template that can wrap arbitary function templates to functors in boost but I didn't find any. Then I tried to write such a wrapper using constructs like template<template<typename T0> class F>struct temp_fun_1; but the code made my MSVC2008 compiler crash(really!). Is there such a wrapper in boost or any other library, or is it even possible? -- View this message in context: http://boost.2283326.n4.nabble.com/fusion-Is-it-possible-to-wrap-a-function-... Sent from the Boost - Users mailing list archive at Nabble.com.
On 8/11/2011 11:28 AM, someguy wrote:
If there is a function template: template<typename T> T f(T t); then f is not a type and cannot be passed to fusion::for_each() as the second argument. But I can't write fusion::for_each(someSequence,&f<SomeType>) either since elements of someSequence has different types. While I can write f as a functor fo struct fo{ template<typename T> T operator()(T t); }; and call fusion::for_each(someSequence,fo()), sometimes I need to use function templates in libraries ( say, I have a fusion::vector whose elements are ranges ) and the only solution seems to be writing a lot of functors to wrap the function templates. I tried to find a wrapper class template that can wrap arbitary function templates to functors in boost but I didn't find any. Then I tried to write such a wrapper using constructs like template<template<typename T0> class F>struct temp_fun_1; but the code made my MSVC2008 compiler crash(really!). Is there such a wrapper in boost or any other library, or is it even possible?
fusion::for_each is easy because the expected function only needs to return void. It gets more complex if you have to return something, e.g. fusion::fold. You really need a Polymorphic Function Object (http://tinyurl.com/3qpsrkc) with proper result_of facilities. Anything less won't cut it. Wrapper? There's nothing close to what you want primarily due to the complexities of the return type deduction. It won't make sense to provide an easy wrapper for monomorphic return types which is only useful for for_each. Regards, -- Joel de Guzman http://www.boostpro.com http://boost-spirit.com
If there is a function template: template<typename T> T f(T t); then f is not a type and cannot be passed to fusion::for_each() as the second argument. But I can't write fusion::for_each(someSequence,&f<SomeType>) either since elements of someSequence has different types. While I can write f as a functor fo struct fo{ template<typename T> T operator()(T t); }; and call fusion::for_each(someSequence,fo()), sometimes I need to use function templates in libraries ( say, I have a fusion::vector whose elements are ranges ) and the only solution seems to be writing a lot of functors to wrap the function templates. I tried to find a wrapper class template that can wrap arbitary function templates to functors in boost but I didn't find any. Then I tried to write such a wrapper using constructs like template<template<typename T0> class F>struct temp_fun_1; but the code made my MSVC2008 compiler crash(really!). Is there such a wrapper in boost or any other library, or is it even possible?
fusion::for_each is easy because the expected function only needs to return void. It gets more complex if you have to return something, e.g. fusion::fold. You really need a Polymorphic Function Object (http://tinyurl.com/3qpsrkc) with proper result_of facilities. Anything less won't cut it.
Wrapper? There's nothing close to what you want primarily due to the complexities of the return type deduction. It won't make sense to provide an easy wrapper for monomorphic return types which is only useful for for_each.
What if we use C++0x decltype for return type deduction? Is it possible then? Thanks, Nate
I changed to void return type and still had my compiler crashed... template<template<typename T0> class F> struct temp_fun_1 { template<typename TT0> void operator()( TT0 a1 ) { // empty function for simplicity } }; template<typename T> void Loopback(T t){ std::cout<<t; } temp_fun_1<Loopback>(); //in main(). I can't even instantiate temp_fun_1. -- View this message in context: http://boost.2283326.n4.nabble.com/fusion-Is-it-possible-to-wrap-a-function-... Sent from the Boost - Users mailing list archive at Nabble.com.
On Aug 11, 2011, at 11:15 PM, someguy wrote:
template<template<typename T0> class F> struct temp_fun_1 { template<typename TT0> void operator()( TT0 a1 ) { // empty function for simplicity } };
template<typename T> void Loopback(T t){ std::cout<<t; }
temp_fun_1<Loopback>(); //in main(). I can't even instantiate temp_fun_1.
(Please post as plain text.) I think the essential problem is that you'd have to have a template parameter which takes a function template, which can't be done: http://groups.google.com/group/comp.std.c++/browse_thread/thread/64738c5af5b...
On 8/12/2011 12:23 PM, Gordon Woodhull wrote:
On Aug 11, 2011, at 11:15 PM, someguy wrote:
template<template<typename T0> class F> struct temp_fun_1 { template<typename TT0> void operator()( TT0 a1 ) { // empty function for simplicity } };
template<typename T> void Loopback(T t){ std::cout<<t; }
temp_fun_1<Loopback>(); //in main(). I can't even instantiate temp_fun_1.
(Please post as plain text.)
I think the essential problem is that you'd have to have a template parameter which takes a function template, which can't be done:
http://groups.google.com/group/comp.std.c++/browse_thread/thread/64738c5af5b...
Just use a plain, ol' macro to make a simple PFO wrapper ;-) Regards, -- Joel de Guzman http://www.boostpro.com http://boost-spirit.com
Joel de Guzman-2 wrote:
Just use a plain, ol' macro to make a simple PFO wrapper ;-)
Thanks for the advice. Now this worked: Though I still wonder whether there are similar macros somewhere in boost. -- View this message in context: http://boost.2283326.n4.nabble.com/fusion-Is-it-possible-to-wrap-a-function-... Sent from the Boost - Users mailing list archive at Nabble.com.
On Friday, August 12, 2011 01:45:39 AM someguy wrote:
Joel de Guzman-2 wrote:
Just use a plain, ol' macro to make a simple PFO wrapper ;-)
Thanks for the advice. Now this worked:
Though I still wonder whether there are similar macros somewhere in boost.
Sorry for the delay. In fact there is a macro in boost doing exactly what you are asking for: http://tinyurl.com/3nmgerk HTH, Thomas
participants (5)
-
Gordon Woodhull
-
Joel de Guzman
-
Nathan Ridge
-
someguy
-
Thomas Heller