[mpl][variant] Visitor with typelists
What is the best (shortest/most elegant) way to create a visitor for a Variant, where there are subsets of the variant types, for which the operation is the same? typedef boost::mpl::vector<A, B, C> v1; typedef boost::mpl::vector<D, E> v2; typedef boost::mpl::joint_view<v1, v2> v; typedef boost::make_variant_over< v >::type v_union; struct visitor : public boost::static_visitor<int> { int operator()([T in v1] arg) { // do something for A, B, C return arg.a; } int operator()([T in v2] arg) { // do something for D, E return arg.b; } }; Thanks, Tilo
AMDG Tilo Nitzsche wrote:
What is the best (shortest/most elegant) way to create a visitor for a Variant, where there are subsets of the variant types, for which the operation is the same?
typedef boost::mpl::vector<A, B, C> v1; typedef boost::mpl::vector<D, E> v2; typedef boost::mpl::joint_view<v1, v2> v; typedef boost::make_variant_over< v >::type v_union;
struct visitor : public boost::static_visitor<int> { int operator()([T in v1] arg) { // do something for A, B, C return arg.a; } int operator()([T in v2] arg) { // do something for D, E return arg.b; } };
Use enable_if typedef boost::mpl::vector<A, B, C> v1; typedef boost::mpl::vector<D, E> v2; typedef boost::mpl::joint_view<v1, v2> v; typedef boost::make_variant_over< v >::type v_union; struct visitor : public boost::static_visitor<int> { template<class T> typename boost::enable_if<boost::mpl::contains<v1, T>, int>::type operator()(T arg) { // do something for A, B, C return arg.a; } template<class T> typename boost::enable_if<boost::mpl::contains<v2, T>, int>::type operator()(T arg) { // do something for D, E return arg.b; } }; In Christ, Steven Watanabe
Thanks! That's certainly better than what I came up with: class visitor : public boost::static_visitor<char*> { public: template<typename T> char* operator()(const T& arg) const { typedef typename mpl::contains<v1, T>::type is_v1; typedef typename mpl::contains<v2, T>::type is_v2; return run(arg, is_v1(), is_v2()); } private: template<typename T> char* run(T arg, mpl::bool_<true>, mpl::bool_<false>) const { return arg.a; } template<typename T> char* run(T arg, mpl::bool_<false>, mpl::bool_<true>) const { return arg.b; } }; -- Tilo --- On Mon, 12/29/08, Steven Watanabe <watanabesj@gmail.com> wrote:
From: Steven Watanabe <watanabesj@gmail.com> Subject: Re: [Boost-users] [mpl][variant] Visitor with typelists To: boost-users@lists.boost.org Date: Monday, December 29, 2008, 10:21 AM AMDG
Tilo Nitzsche wrote:
What is the best (shortest/most elegant) way to create a visitor for a Variant, where there are subsets of the variant types, for which the operation is the same?
typedef boost::mpl::vector<A, B, C> v1; typedef boost::mpl::vector<D, E> v2; typedef boost::mpl::joint_view<v1, v2> v; typedef boost::make_variant_over< v >::type v_union;
struct visitor : public boost::static_visitor<int> { int operator()([T in v1] arg) { // do something for A, B, C return arg.a; } int operator()([T in v2] arg) { // do something for D, E return arg.b; } };
Use enable_if
typedef boost::mpl::vector<A, B, C> v1; typedef boost::mpl::vector<D, E> v2; typedef boost::mpl::joint_view<v1, v2> v; typedef boost::make_variant_over< v >::type v_union;
struct visitor : public boost::static_visitor<int> { template<class T> typename boost::enable_if<boost::mpl::contains<v1, T>, int>::type operator()(T arg) { // do something for A, B, C return arg.a; } template<class T> typename boost::enable_if<boost::mpl::contains<v2, T>, int>::type operator()(T arg) { // do something for D, E return arg.b; } };
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Steven Watanabe
-
Tilo Nitzsche