带数组的BOOST_PP_REPEAT [英] BOOST_PP_REPEAT with array

查看:649
本文介绍了带数组的BOOST_PP_REPEAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的结构:

struct E1
{
    typedef boost::tuple<  
     boost::optional< N::type_A >, // N - namespace
     boost::optional< N::type_B >,
     ...................
     boost::optional< N::type_X > //arbitrary number of, maximal is 7
     > data_type;


   // for access by name 
   boost::optional<N::type_A> const&  type_A() const { return boost::get<0>(data); }
   boost::optional<N::type_B> const&  type_B() const { return boost::get<1>(data); }
   .....................................
   boost::optional<N::type_X> const&  type_X() const { return boost::get<2>(data); }

   data_type data;
};

问:我如何使用BOOST预处理器创建此结构?对我来说只有type_A,type_B,...,type_X类型的名称。

Q: how I may create this structure using BOOST preprocessors? For me known only type_A, type_B, ..., type_X name of types.

它需要我,因为我必须创建很多这样的结构,只改变type_A ,type_B,... types。

It's need me, because I must create a lot of structures like that, only changing type_A, type_B, ... types.

在通常情况下,我可以使用boost预处理器数组或设置?

In common case, can I use boost preprocessor array or set?

推荐答案

您可以这样做:

#define TYPES (type_A)(type_B)(type_X)


#define GENERATE_TUPLE(maR, maNamespace, maIndex, maType) \
  BOOST_PP_COMMA_IF(maIndex) boost::optional<maNamespace :: maType>


#define GENERATE_GETTER(maR, maNamespace, maIndex, maType) \
  boost::optional<maNamespace :: maType> const& maType () const { return boost::get<maIndex>(data); }


struct E1
{
  typedef boost::tuple<
    BOOST_PP_SEQ_FOR_EACH_I(GENERATE_TUPLE, N, TYPES)
  > data_type;

  BOOST_PP_SEQ_FOR_EACH_I(GENERATE_GETTER, N, TYPES)

  data_type data;
};

N 参数对应于 maNamespace 参数。你当然可以以任何其他方式使用这个参数(它只是通过逐字传递),例如将 N 硬编码到宏中(并在参数中传递一个虚拟)编码,甚至其中的标识符 data 等。

The N argument corresponds to the maNamespace parameter. You can of course use this argument in any other way (it's just passed through verbatim), such as hardcoding N into the macros (and passing a dummy in the argument), or encoding even the identifier data in there, etc.

这篇关于带数组的BOOST_PP_REPEAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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