如何循环使用非默认构造类的boost MPL列表? [英] How do I loop over a boost MPL list of non-default constructed classes?

查看:150
本文介绍了如何循环使用非默认构造类的boost MPL列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例:

  #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());
}

如果我没有在列表中包含第五个对象, ,但一旦我这样做,我得到以下错误:

 在/ 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:在构造函数'boost :: initialized< T> :: wrapper :: wrapper()[with T = five]':
/usr/local/include/boost_1_56_0/boost/utility/value_init.hpp:109:7:从'boost :: initialized实例化; T> :: initialized()[with T = five]'
/usr/local/include/boost_1_56_0/boost/utility/value_init.hpp:205:12:从'boost :: value_initialized< T& :: value_initialized()[with T = five]'
/usr/local/include/boost_1_56_0/boost/mpl/for_each.hpp:81:9:递归实例化为'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, >,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:实例化为'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 /局部/包括/ boost_1_56_0 / boost / mpl / for_each.hpp:104:5:从'void boost :: mpl :: for_each(F,Sequence *,TransformOp *)实例化[with Sequence = boost :: mpl :: list5&一个,两个,三个,四个,五个,TransformOp = boost :: mpl :: identity / usr / local / include / boost_1_56_0 / boost / mpl / for_each .hpp:118:3:从'void boost :: mpl :: for_each(F,Sequence *)实例化[with Sequence = boost :: mpl :: list5<一,二,三,四,五>,F = type_printer ]'
mpldef.cpp:37:48:从这里实例化
/usr/local/include/boost_1_56_0/boost/utility/value_init.hpp:77:12:错误:使用删除的函数'five :: five()'
mpldef.cpp:10:15:error:declaration here

有什么办法这样做吗?我尝试从 type_printer 调用操作符中删除 T ,所以它看起来像这样: void

解决方案

我已经解决了这个问题添加类型包装结构,例如 Boost.Hana中显示的一个。我添加了这个在提到使用 boost :: mpl :: transform 自动将这个包装类型添加到列表中的每个项目。 / p>

以下详细信息我的解决方案:

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

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

使用这两个新类型,我改变了 type_printer 函子看起来像这样:

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

并且 boost :: mpl :: for_each call in main现在看起来像这样:

  using boost :: mpl :: _ 
using wrapped_list = boost :: mpl :: transform< type_list,add_type_wrapper< _1>> :: type;
boost :: mpl :: for_each< wrapped_list>(type_printer());感谢您的帮助,我认为这是一个非常好的和优雅的解决方案。

感谢您的帮助,我认为这是一个非常好的和优雅的解决方案。


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.

解决方案

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.

这篇关于如何循环使用非默认构造类的boost MPL列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆