variant<> number of template arguments
Hello, I have a use of the visitor pattern (this was an answer provided to me on c++-moderated) as shown below. Question1: is the maximum number of variant<> arguments extendable? Question2: Below, some of the variants I have are full template specs. Is there a way to write the variant<> typedef with templates instead and have the operator() of the static visitor a template member. Rds, --------------- #include <iostream> #include <boost/variant.hpp> struct derived1 {}; struct derived2 {}; struct derived3 {}; typedef boost::variant<derived1, derived2, derived3> base; // function object struct operation : boost::static_visitor<int> { int operator()(const derived1&) const { return 1; } int operator()(const derived2&) const { return 2; } int operator()(const derived3&) const { return 3; } }; int main() { base a = derived1(); operation op; std::cout << a.apply_visitor(op) << std::endl; // prints "1" base a = derived2(); std::cout << a.apply_visitor(op) << std::endl; // prints "2" }
AMDG Hicham Mouline wrote:
Question1: is the maximum number of variant<> arguments extendable?
The easiest way is to use make_variant_over which takes an MPL sequence.
Question2: Below, some of the variants I have are full template specs.
Is there a way to write the variant<> typedef with templates instead and have
the operator() of the static visitor a template member.
I'm not sure what you want. If the members of the variant have a common interface, (For example if they all have a nested integer constant called value) Then you can make the static visitor operator() a template and use this common interface. In Christ, Steven Watanabe
Thank you, Trying to define the variant, I have: typedef boost::mpl::vector<> variant_template_arguments; typedef boost::mpl::vector<constant, linear, polynomial, cubic_spline> interpolator_types; typedef boost::mpl::vector<simpson> integrator_types; I have a curve template template <typename intepolator, typename extrapolator, typename integrator> curve; I want to generate the list of all curves by iterating the template args in the vector<> and inserting these curves in variant_template_arguments to finally typedef make_variant_over< variant_template_arguments >::type my_wanted_variant; How should I do the iteration/insertion in variant_template_arguments? Rds, -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 29 May 2008 23:53 To: boost-users@lists.boost.org Subject: Re: [Boost-users] variant<> number of template arguments AMDG Hicham Mouline wrote:
Question1: is the maximum number of variant<> arguments extendable?
The easiest way is to use make_variant_over which takes an MPL sequence.
Question2: Below, some of the variants I have are full template specs.
Is there a way to write the variant<> typedef with templates instead and have
the operator() of the static visitor a template member.
I'm not sure what you want. If the members of the variant have a common interface, (For example if they all have a nested integer constant called value) Then you can make the static visitor operator() a template and use this common interface. In Christ, Steven Watanabe
After some research, I found this post: http://lists.boost.org/boost-users/2007/05/27648.php that provides a 2-sequence cartesian product metafunction that I will try to use to have the product of the 3 sequences I have. Rds, -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 29 May 2008 23:53 To: boost-users@lists.boost.org Subject: Re: [Boost-users] variant<> number of template arguments AMDG Hicham Mouline wrote:
Question1: is the maximum number of variant<> arguments extendable?
The easiest way is to use make_variant_over which takes an MPL sequence.
Question2: Below, some of the variants I have are full template specs.
Is there a way to write the variant<> typedef with templates instead and have
the operator() of the static visitor a template member.
I'm not sure what you want. If the members of the variant have a common interface, (For example if they all have a nested integer constant called value) Then you can make the static visitor operator() a template and use this common interface. In Christ, Steven Watanabe
AMDG
Hicham Mouline wrote:
Question1: is the maximum number of variant<> arguments extendable?
The easiest way is to use make_variant_over which takes an MPL sequence.
It seems make_variant_over is also limited by BOOST_VARIANT_LIMIT_TYPES. This macro is 20 on 2 platforms (linux-i386-gcc-4.1.2-boost-1.34.1 and win-i386-vs2005- boost-1.35.0) on both platforms, #include <iostream> #include <boost/mpl/vector.hpp> #include <boost/variant.hpp> class type0 {}; class type1 {}; class type2 {}; class type3 {}; class type4 {}; class type5 {}; class type6 {}; class type7 {}; class type8 {}; class type9 {}; class type10 {}; class type11 {}; class type12 {}; class type13 {}; class type14 {}; class type15 {}; class type16 {}; class type17 {}; class type18 {}; class type19 {}; int main() { typedef boost::mpl::vector< int, type0, type1, type2, type3, type4, type5, type6, type7, type8, type9, type10, type11, type12, type13, type14, type15, type16, type17, type18 > types; // vector with 20 types, builds ok. boost::make_variant_over<types>::type v = 5; std::cout<< boost::get<int>(v) <<std::endl; } If I add type19, making the mpc vector have 21 types, compilation fails... Rds,
AMDG Hicham Mouline wrote:
It seems make_variant_over is also limited by BOOST_VARIANT_LIMIT_TYPES. This macro is 20 on 2 platforms (linux-i386-gcc-4.1.2-boost-1.34.1 and win-i386-vs2005- boost-1.35.0)
<snip>
If I add type19, making the mpc vector have 21 types, compilation fails...
The limit of variant and the limit of mpl::vector happen to be the same. For more types, try using the numbered form of mpl vector which can handle up to 50 arguments. #include <boost/mpl/vector/vector30.hpp> typedef boost::mpl::vector21<...> types; In Christ, Steven Watanabe
This works thank you very much steven, I am now trying to implement the cartesian product of 3 sequences based on the earlier post: template<typename T> struct pair_with : mpl::pair<T,boost::mpl::_> {}; template<typename Sequence1,typename Sequence2, typename Sequence3> struct sequence_product : boost::mpl::fold< Sequence1, boost::mpl::vector0<>, boost::mpl::joint_view< boost::mpl::_1, boost::mpl::transform< Sequence2, pair_with<boost::mpl::_2> > >
{}; Rds, -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 31 May 2008 20:51 To: boost-users@lists.boost.org Subject: Re: [Boost-users] variant<> number of template arguments AMDG Hicham Mouline wrote:
It seems make_variant_over is also limited by BOOST_VARIANT_LIMIT_TYPES. This macro is 20 on 2 platforms (linux-i386-gcc-4.1.2-boost-1.34.1 and win-i386-vs2005- boost-1.35.0)
<snip>
If I add type19, making the mpc vector have 21 types, compilation fails...
The limit of variant and the limit of mpl::vector happen to be the same. For more types, try using the numbered form of mpl vector which can handle up to 50 arguments. #include <boost/mpl/vector/vector30.hpp> typedef boost::mpl::vector21<...> types; In Christ, Steven Watanabe
Hello, 1) I now managed to produce the cartesian product of 3 mpl sequences applying my custom metafunction to the resulting triplets, however it takes like 40seconds to compile. I doubt this is the optimal way to write the cartesian product metafunction. (code compiles as-is, below) 2) By the way, cartesian product hasn't been deemed generic enough to be included in the MPL? 3) what would be the equivalent of mpl::pair for 3 or more? boost::tuple or boost::variant? Your help is very appreciated, thank you, #include <iostream> #include <boost/mpl/pair.hpp> #include <boost/mpl/placeholders.hpp> #include <boost/mpl/fold.hpp> #include <boost/mpl/vector/vector0.hpp> #include <boost/mpl/vector.hpp> #include <boost/mpl/joint_view.hpp> #include <boost/mpl/transform.hpp> #include <boost/mpl/for_each.hpp> #include <boost/mpl/size.hpp> #include <boost/variant.hpp> template <typename inter, typename extra, typename integ> class curve {}; template <typename inter, typename extra, typename integ> struct curve_metafunction { typedef curve<inter,extra,integ> type; }; template<typename T> struct pair_with : boost::mpl::pair<T,boost::mpl::_> {}; template<typename pair> struct triple_with : curve_metafunction < typename pair::first, typename pair::second, boost::mpl::_ > {}; template<typename Sequence1,typename Sequence2> struct sequence_product : boost::mpl::fold< Sequence1, boost::mpl::vector0<>, boost::mpl::joint_view< boost::mpl::_1, boost::mpl::transform< Sequence2, pair_with<boost::mpl::_2> > >
{}; template<typename SequencePair, typename Sequence> struct seq_seqpair_product : boost::mpl::fold< SequencePair, boost::mpl::vector0<>, boost::mpl::joint_view< boost::mpl::_1, boost::mpl::transform< Sequence, triple_with<boost::mpl::_2> > >
{}; template<typename Sequence1,typename Sequence2,typename Sequence3> struct seq_product : seq_seqpair_product< sequence_product<Sequence1,Sequence2>::type, Sequence3 > {}; typedef boost::mpl::vector<constant, linear, polynomial, cubic_spline> interpolator_types; typedef interpolator_types extrapolator_types; typedef boost::mpl::vector<simpson> integrator_types; typedef seq_product<interpolator_types, extrapolator_types,integrator_types>::type curve_types; struct type_printer { template <typename T> void operator()( T ) { std::cout<< typeid(T).name() <<std::endl; } }; int main() { std::cout<< boost::mpl::size<curve_types>::type::value <<std::endl; boost::mpl::for_each<curve_types>( type_printer() ); } -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 31 May 2008 20:51 To: boost-users@lists.boost.org Subject: Re: [Boost-users] variant<> number of template arguments AMDG Hicham Mouline wrote:
It seems make_variant_over is also limited by BOOST_VARIANT_LIMIT_TYPES. This macro is 20 on 2 platforms (linux-i386-gcc-4.1.2-boost-1.34.1 and win-i386-vs2005- boost-1.35.0)
<snip>
If I add type19, making the mpc vector have 21 types, compilation fails...
The limit of variant and the limit of mpl::vector happen to be the same. For more types, try using the numbered form of mpl vector which can handle up to 50 arguments. #include <boost/mpl/vector/vector30.hpp> typedef boost::mpl::vector21<...> types; In Christ, Steven Watanabe
participants (2)
-
Hicham Mouline
-
Steven Watanabe