Hi there, since there is no example in the docs and no test for it. I was wondering how to use fusion's transform metafunction. Here is what I want to do. template <typename T> struct add_vector { typedef std::vector<T> type; }; struct A; struct B; struct C; int main() { typedef fusion::list< A, B, C > types; typedef fusion::transform< types, add_vector<> >::type vector_of_types; return 0; } The compiler is complaining that there are too few parameters for add_vector. In MPL you would just add _1 as the template parameter. But this doesn't work for fusion. So, how do I use it? Thanks, Christian
On 3/16/07, Christian Henning <chhenning@gmail.com> wrote:
Hi there, since there is no example in the docs and no test for it. I was wondering how to use fusion's transform metafunction.
Here is what I want to do.
template <typename T> struct add_vector { typedef std::vector<T> type; };
struct A; struct B; struct C;
int main() { typedef fusion::list< A, B, C > types; typedef fusion::transform< types, add_vector<> >::type vector_of_types;
return 0; }
The compiler is complaining that there are too few parameters for add_vector. In MPL you would just add _1 as the template parameter. But this doesn't work for fusion. So, how do I use it?
You need your function add_vector to be a Polymorphic function object. I think you need something like this: struct add_vector { template <typename T> struct result { typedef std::vector<T> type; }; }; typedef typename boost::fusion::result_of::transform<types, add_vector>::type tresult; Chris
Christian Henning wrote:
Hi there, since there is no example in the docs and no test for it. I was wondering how to use fusion's transform metafunction.
Here is what I want to do.
template <typename T> struct add_vector { typedef std::vector<T> type; };
struct A; struct B; struct C;
int main() { typedef fusion::list< A, B, C > types; typedef fusion::transform< types, add_vector<> >::type vector_of_types;
return 0; }
The compiler is complaining that there are too few parameters for add_vector. In MPL you would just add _1 as the template parameter. But this doesn't work for fusion. So, how do I use it?
There is an example (at least a test). You can get that here: libs/fusion/test/algorithm/transform/transform.cpp. The doc also contains an example (see the transform docs) This is one of the cases where Fusion differs from MPL. Fusion requires a polymorphic function object for the transform. See the docs on "Polymorphic Function Object". Having said that, and looking at your code, it seems you do not need value transformation (which is what Polymorphic Function Objects do). All you are doing is type manipulation. I'd suggest you to first work on an mpl sequence. Then, when you get your desired MPL sequence, convert it to a fusion sequence through fusion::as_xxx (see conversions in the docs). fusion::as_xxx can take in any valid MPL sequence. Here's an example: typedef fusion::result_of::as_list<any_mpl_seq>::type list_of_vectors; Regards, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
Thanks Chris and Joel for your replies.
There is an example (at least a test). You can get that here: libs/fusion/test/algorithm/transform/transform.cpp. The doc also contains an example (see the transform docs)
I was looking at this test. But it doesn't include a test of the transform metafunction, I believe. Correct me if I'm wrong.
This is one of the cases where Fusion differs from MPL. Fusion requires a polymorphic function object for the transform. See the docs on "Polymorphic Function Object".
I would suggest to add an example on the "Polymorphic Function Object" page in the documentation. Also, how about adding an index at the end of the documentation? I would like that a lot.
Having said that, and looking at your code, it seems you do not need value transformation (which is what Polymorphic Function Objects do). All you are doing is type manipulation. I'd suggest you to first work on an mpl sequence. Then, when you get your desired MPL sequence, convert it to a fusion sequence through fusion::as_xxx (see conversions in the docs). fusion::as_xxx can take in any valid MPL sequence. Here's an example:
typedef fusion::result_of::as_list<any_mpl_seq>::type list_of_vectors;
This is what I'm doing right now: #include <vector> #include <boost/mpl/assert.hpp> #include <boost/mpl/equal.hpp> #include <boost/mpl/list.hpp> #include <boost/mpl/transform.hpp> #include <boost/fusion/algorithm/transformation/transform.hpp> #include <boost/fusion/algorithm/iteration/for_each.hpp> #include <boost/fusion/sequence/container.hpp> #include <boost/fusion/sequence/conversion/as_list.hpp> #include <boost/type_traits/add_pointer.hpp> using namespace std; using namespace boost; template <typename T> struct add_vector { typedef std::vector<T> type; }; struct A; struct B; struct C; int main() { typedef mpl::list< A, B, C > types; typedef mpl::transform1< types, add_vector<mpl::_1> >::type mpl_list_of_vectors; typedef fusion::result_of::as_list<mpl_list_of_vectors>::type fusion_list_of_vectors; return 0; } Unfortunately, this doesn't compile. I'm using VC 7.1. hpp(33) : error C2504: 'boost::fusion::extension::begin_impl<Tag>::apply<Sequence>' : base class undefined with [ Tag=boost::fusion::traits::tag_of<boost::remove_const<mpl_list_of_vectors>::type>::type ] and [ Sequence=mpl_list_of_vectors ] c:\boost\boost\fusion\sequence\intrinsic\begin.hpp(33) : see reference to class template instantiation 'boost::fusion::extension::begin_impl<Tag>::apply<Sequence>' being compiled with [ Tag=boost::fusion::traits::tag_of<boost::remove_const<mpl_list_of_vectors>::type>::type, Sequence=mpl_list_of_vectors ] c:\boost\boost\fusion\sequence\conversion\as_list.hpp(26) : see reference to class template instantiation 'boost::fusion::result_of::begin<Sequence>' being compiled with [ Sequence=mpl_list_of_vectors ] etc. Is there anything wrong with my code? Christian
Christian Henning wrote:
I was looking at this test. But it doesn't include a test of the transform metafunction, I believe. Correct me if I'm wrong.
test/algorithm/transform.cpp
I would suggest to add an example on the "Polymorphic Function Object" page in the documentation. Also, how about adding an index at the end of the documentation? I would like that a lot.
That would be nice. It's a quickbook/boostbook todo item in the queue. [snips]
This is what I'm doing right now:
[snips] Add: #include <boost/fusion/sequence/adapted/mpl.hpp> Regards, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
Thanks Joel. On 3/19/07, Joel de Guzman <joel@boost-consulting.com> wrote:
Christian Henning wrote:
I was looking at this test. But it doesn't include a test of the transform metafunction, I believe. Correct me if I'm wrong.
test/algorithm/transform.cpp
I would suggest to add an example on the "Polymorphic Function Object" page in the documentation. Also, how about adding an index at the end of the documentation? I would like that a lot.
That would be nice. It's a quickbook/boostbook todo item in the queue.
[snips]
This is what I'm doing right now:
[snips]
Add:
#include <boost/fusion/sequence/adapted/mpl.hpp>
Regards, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Chris Weed
-
Christian Henning
-
Joel de Guzman