[iterator || function_types] zip_iterator -> unpack and call?
Hi, Say I have a function "int add(int, int)" and want to use it in a call to std::transform with zip_iterators. Is there a way to do it without writing an intermediate function object? If not, would it be sensible to write a generic one using the function_types library? I can't imagine this would be too hard but just wanted to know if there is anything I can leverage before writing it myself. Thanks in advance, Sohail
Sohail Somani wrote:
Hi,
Say I have a function "int add(int, int)" and want to use it in a call to std::transform with zip_iterators. Is there a way to do it without writing an intermediate function object?
If not, would it be sensible to write a generic one using the function_types library? I can't imagine this would be too hard but just wanted to know if there is anything I can leverage before writing it myself.
I guess what you're looking for is... http://tinyurl.com/26t9dd =http://lists.boost.org/Archives/boost/2007/01/115135.php http://tinyurl.com/y9ax7t =http://boost.cvs.sourceforge.net/boost/boost/boost/iterator/zip_iterator.hpp?view=log&pathrev=zip_iterator_fusion It seems not so easy because of the Forwarding Problem. Regards, -- Shunsuke Sogame
Thank you, I'll take a look. -----Original Message----- From: boost-users-bounces@lists.boost.org on behalf of shunsuke Sent: Fri 2/16/2007 7:36 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [iterator || function_types] zip_iterator ->unpack and call? Sohail Somani wrote:
Hi,
Say I have a function "int add(int, int)" and want to use it in a call to std::transform with zip_iterators. Is there a way to do it without writing an intermediate function object?
If not, would it be sensible to write a generic one using the function_types library? I can't imagine this would be too hard but just wanted to know if there is anything I can leverage before writing it myself.
I guess what you're looking for is... http://tinyurl.com/26t9dd =http://lists.boost.org/Archives/boost/2007/01/115135.php http://tinyurl.com/y9ax7t =http://boost.cvs.sourceforge.net/boost/boost/boost/iterator/zip_iterator.hpp?view=log&pathrev=zip_iterator_fusion
shunsuke wrote:
Sohail Somani wrote:
Hi,
Say I have a function "int add(int, int)" and want to use it in a call to std::transform with zip_iterators. Is there a way to do it without writing an intermediate function object?
If not, would it be sensible to write a generic one using the function_types library? I can't imagine this would be too hard but just wanted to know if there is anything I can leverage before writing it myself.
I guess what you're looking for is...
http://tinyurl.com/26t9dd =http://lists.boost.org/Archives/boost/2007/01/115135.php
http://tinyurl.com/y9ax7t =http://boost.cvs.sourceforge.net/boost/boost/boost/iterator/zip_iterator.hpp?view=log&pathrev=zip_iterator_fusion
It seems not so easy because of the Forwarding Problem.
Forwarding doesn't seem a problem to me since you already have the argument tuple. The return type inference is painful to get right, though. On the surface it seems as easy as template<class F, class Tp> ... unpack_and_call( F f, Tp const & tp ) { return unpack_and_call_( tuple_size<Tp>(), f, tp ); } template<class F, class Tp> ... unpack_and_call_( mpl::int_<0>, F f, Tp const & tp ) { return f(); } template<class F, class Tp> ... unpack_and_call_( mpl::int_<1>, F f, Tp const & tp ) { return f( get<0>(tp) ); } // and so on but the ... part is more involved if one decides to use result_of. boost::bind-style inference is of course easier since it only needs to look at F.
Peter Dimov wrote:
shunsuke wrote:
Sohail Somani wrote:
Hi,
Say I have a function "int add(int, int)" and want to use it in a call to std::transform with zip_iterators. Is there a way to do it without writing an intermediate function object?
If not, would it be sensible to write a generic one using the function_types library? I can't imagine this would be too hard but just wanted to know if there is anything I can leverage before writing it myself. I guess what you're looking for is...
http://tinyurl.com/26t9dd =http://lists.boost.org/Archives/boost/2007/01/115135.php
http://tinyurl.com/y9ax7t =http://boost.cvs.sourceforge.net/boost/boost/boost/iterator/zip_iterator.hpp?view=log&pathrev=zip_iterator_fusion
It seems not so easy because of the Forwarding Problem.
Forwarding doesn't seem a problem to me since you already have the argument tuple. The return type inference is painful to get right, though. On the surface it seems as easy as
template<class F, class Tp> ... unpack_and_call( F f, Tp const & tp ) { return unpack_and_call_( tuple_size<Tp>(), f, tp ); }
template<class F, class Tp> ... unpack_and_call_( mpl::int_<0>, F f, Tp const & tp ) { return f(); }
template<class F, class Tp> ... unpack_and_call_( mpl::int_<1>, F f, Tp const & tp ) { return f( get<0>(tp) ); }
// and so on
but the ... part is more involved if one decides to use result_of. boost::bind-style inference is of course easier since it only needs to look at F.
Right. I have ever written such codes :-) I should say " *pack*_and_call is not so easy. ". Regards, -- Shunsuke Sogame
participants (3)
-
Peter Dimov
-
shunsuke
-
Sohail Somani