How do I loop over a boost MPL list of non-default

2019-04-10 14:30发布

问题:

I have the following example:

#include <iostream>

#include <boost/mpl/for_each.hpp>
#include <boost/mpl/list.hpp>

struct one {};
struct two {};
struct three {};
struct four {};
struct five { five() = delete; };

template <typename T>
void print()
{
  std::cout << "hello " << typeid(T).name() << std::endl;
}

struct type_printer
{
  template <typename T>
  void operator()(T)
  {
    print<T>();
  }
};

int main()
{
  typedef boost::mpl::list<
    one,
    two,
    three,
    four,
    five
  >::type type_list;

  boost::mpl::for_each<type_list>(type_printer());
}

Which works absolutely fine if I don't include the fifth object in the list, but once I do I get the following errors:

In file included from /usr/local/include/boost_1_56_0/boost/mpl/for_each.hpp:29:0,
                 from mpldef.cpp:3:
/usr/local/include/boost_1_56_0/boost/utility/value_init.hpp: In constructor ‘boost::initialized<T>::wrapper::wrapper() [with T = five]’:
/usr/local/include/boost_1_56_0/boost/utility/value_init.hpp:109:7:   instantiated from ‘boost::initialized<T>::initialized() [with T = five]’
/usr/local/include/boost_1_56_0/boost/utility/value_init.hpp:205:12:   instantiated from ‘boost::value_initialized<T>::value_initialized() [with T = five]’
/usr/local/include/boost_1_56_0/boost/mpl/for_each.hpp:81:9:   recursively instantiated from ‘static void boost::mpl::aux::for_each_impl<false>::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter<boost::mpl::list4<two, three, four, five> >, LastIterator = boost::mpl::l_iter<boost::mpl::l_end>, TransformFunc = boost::mpl::identity<mpl_::na>, F = type_printer]’
/usr/local/include/boost_1_56_0/boost/mpl/for_each.hpp:81:9:   instantiated from ‘static void boost::mpl::aux::for_each_impl<false>::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter<boost::mpl::list5<one, two, three, four, five> >, LastIterator = boost::mpl::l_iter<boost::mpl::l_end>, TransformFunc = boost::mpl::identity<mpl_::na>, F = type_printer]’
/usr/local/include/boost_1_56_0/boost/mpl/for_each.hpp:104:5:   instantiated from ‘void boost::mpl::for_each(F, Sequence*, TransformOp*) [with Sequence = boost::mpl::list5<one, two, three, four, five>, TransformOp = boost::mpl::identity<mpl_::na>, F = type_printer]’
/usr/local/include/boost_1_56_0/boost/mpl/for_each.hpp:118:3:   instantiated from ‘void boost::mpl::for_each(F, Sequence*) [with Sequence = boost::mpl::list5<one, two, three, four, five>, F = type_printer]’
mpldef.cpp:37:48:   instantiated from here
/usr/local/include/boost_1_56_0/boost/utility/value_init.hpp:77:12: error: use of deleted function ‘five::five()’
mpldef.cpp:10:15: error: declared here

Is there any way of doing this? I tried removing T from the type_printer call operator, so it looked something like this: void operator()() and I still got the same error.

回答1:

I've solved this by adding a type wrapper struct, like the one seen in Boost.Hana. I added this to the great idea found in this comment that mentions using boost::mpl::transform to add this wrapper type to each item in the list automatically.

The following details my solution:

template <typename T>
struct type_
{
  using type = T;
};

template <typename T>
struct add_type_wrapper
{
  using type = type_<T>;
};

Using these two new types, I altered the type_printer functor to look like so:

struct type_printer
{
  template <typename T>
  void operator()(T)
  {
    using type_t = typename T::type;
    print<type_t>();
  }
};

And the boost::mpl::for_each call in main now looks like this:

using boost::mpl::_1;
using wrapped_list = boost::mpl::transform<type_list, add_type_wrapper<_1>>::type;
boost::mpl::for_each<wrapped_list>(type_printer());

Thanks for your help guys, I think this is a really nice and elegant solution.



回答2:

You can also add one level of indirection to items in your type list like this:

template <typename T>
struct type_ref
{
  typedef T type;
}

struct type_printer
{
  template <typename T>
  void operator()(T)
  {
    print<T>();
  }
  template <typename T>
  void operator()(type_ref<T>)
  {
    print<T>();
  }
};

int main()
{
  typedef boost::mpl::list<
    one,
    two,
    three,
    four,
    type_ref<five>
  >::type type_list;

  boost::mpl::for_each<type_list>(type_printer());
}


回答3:

You may do yourself something like:

namespace detail {

    template <typename Tuple>
    structure my_for_each_t;

    template <typename... Ts>
    structure my_for_each_t<boost::mpl::list<Ts...>>
    {
        template <typename F>
        void operator()(F f) const
        {
             initializer_list<int>{(static_cast<void>(f<Ts>()), 0)...};
        }
    };

}

template <typename Tuple, typename F>
void my_for_each(F f)
{
    detail::my_for_each_t<Tuple>()(f);
}

Or transform your list to add pointer to the type, and change your type_printer:

struct type_printer
{
  template <typename T>
  void operator()(T*) // pointer added.
  {
    print<T>();
  }
};