MSVC 2010中的递归可变参数宏

时间:2019-06-28 05:02:48

标签: c++ visual-studio-2010 variadic-macros visual-studio-macros

很遗憾,我正在使用Visual Studio 2010,并且无法使用constexpr等c ++ 11功能。这很不幸,因为我们试图做一些编译时的技巧,以简化源代码开发并在编译时/在智能感知中捕获代码库中的一些常见错误。

我实质上是在创建代码完成兼容的映射结构。使用模板参数创建的一系列对象包含引用命名空间中其他静态对象的对象。

到目前为止,当映射不可能或无效时,这会使构建失败。如果不存在映射的对象,则不可能在映射的对象中定位,或者如果提供了太多或两个,则错误将非常清楚地显示出来。这是我需要保持的行为。

我已经创建了一个宏,该宏有助于摆脱样板,并且效果很好。但是,对于某些人来说,初始化仍然有些冗长,因为必须为每行单独填写每个子对象。

我们想要创建一个宏,该宏允许将要放入映射结构中的子对象的连续行放置在一行中。

我一直在尝试创建一个递归可变参数宏,该宏扩展对子对象和位置列表的单个引用。理想情况下,仅可以指定位置枚举中的最小值和最大值,但是完整列表也将朝着正确的方向发展。

//Types

enum otherPosition{
   p0
   p1,
   p2,
   p3,
   p4,
   maxP
};

OtherObject other1, other2, other3;

class Sub{
     Sub(OtherObect& mapped, const otherPosition mappedPosition); 
...
};

template<size_t SIZE> 
class ContainerObject{
     ContainerObject(boost::array<Sub,SIZE>& mappedObjects); 
...
};

//Raw initialization

ContainerObject<5> objectA( 
     boost::array<5> inputs = { 
          Sub(other1, p0),
          Sub(other1, p1),
          Sub(other1, p2),
          Sub(other2, p0),
          Sub(other3, p3)
});

//Using Macro

MAKE_CONTAINER(ObjectA, 5, {
          Sub(other1, p0),
          Sub(other1, p1),
          Sub(other1, p2),
          Sub(other2, p0),
          Sub(other3, p3)
});

//How we want it to work

MAKE_CONTAINER(ObjectA, 5, {
          CONSECUTIVE_SUB(other1, p0, p2),
          Sub(other2, p0),
          Sub(other3, p3)
});

我不知道如何使此CONSECUTIVE_SUB宏正常工作。似乎拒绝正确扩展到

Sub(other1, p0), 

更不用说

Sub(other1, p0), Sub(other1, p1), Sub(other1,p2),

有人知道如何做到这一点吗?

0 个答案:

没有答案
相关问题