C++:在不传递一百万个参数的情况下分解代码



我正在进行C/C++编程(主要是C++),我发现自己需要分解实际上是原来两倍的代码,只是每次出现的"left"都被"right"替换。一旦代码完成,我需要知道执行的是"左"版本还是"右"版本,但仅此而已,两者都会返回一个我可以理解的数字(一旦与左或右信息组合在一起)。

在这种设置中,每次更改都需要进行两次,这很烦人。

因此,我可以简单地通过用"other"替换left/right来进行因子分解,并调用因子分解函数两次,每次都知道我调用它是"left"还是"right"。

现在,当我们到达代码的这一部分时,已经有一百万个变量(游标、ID、正在填充的数组等…)。因此,如果我想对左/右代码进行分解,我需要函数有大量的参数,这看起来很难看。

我也不想用只在这种情况下使用的属性重载我的C++类。

有什么建议可以在这里顺利分解吗?

int arrayRight[many], arrayLeft[many], cursor;
while(1)
{
rightThing = arrayRight[cursor];
// Process with RightThing assigned
// ...
// ...
// ...
leftThing = arrayLeft[cursor]
// Process with RightThing assigned
// ...
// ...
// ...
cursor++;
}

试试这个?(它只在C++14上工作,因为它使用自动lambda)

auto func = [&](auto& theThing){
// blah, blah code
};
func(arrayRight[cursor]);
func(arrayLeft [cursor]);

这里的[&]表示将同一范围内的所有变量导入lambda函数。

对于较旧的C++版本,我使用以下代码作为一种丑陋的方式(在一所学校的C99项目中)。

int* pData[2] = {arrayRight, arrayLeft};
for (int i=0; i<2; i++)
{
int* theThing = pData[i];
// blah, blah
}

我的一些朋友告诉我宏可以容纳VA_ARGS,所以这里有一个宏方法。它应该在GCC方面发挥作用。但对于MSVC 2003来说,这并不奏效。(__typeof需要替换为boost::typeof,旧版本的MSVC不支持匿名结构定义)

#define in(...) __VA_ARGS__
#define PP_ARG_N( 
_1,  _2,  _3,  _4,  _5,  _6,  _7,  _8,  _9, _10, 
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, 
_21, _22, _23, _24, _25, _26, _27, _28, _29, _30, 
_31, _32, _33, _34, _35, _36, _37, _38, _39, _40, 
_41, _42, _43, _44, _45, _46, _47, _48, _49, _50, 
_51, _52, _53, _54, _55, _56, _57, _58, _59, _60, 
_61, _62, _63, N, ...) N
#define PP_RSEQ_N()                                        
63, 62, 61, 60,                                   
59, 58, 57, 56, 55, 54, 53, 52, 51, 50,           
49, 48, 47, 46, 45, 44, 43, 42, 41, 40,           
39, 38, 37, 36, 35, 34, 33, 32, 31, 30,           
29, 28, 27, 26, 25, 24, 23, 22, 21, 20,           
19, 18, 17, 16, 15, 14, 13, 12, 11, 10,           
9,  8,  7,  6,  5,  4,  3,  2,  1,  0
#define PP_NARG_(...)    PP_ARG_N(__VA_ARGS__)
#define PP_NARG(...)     PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
#define withInternal(dataType, desiredType, x, dataCnt, data...) for(struct {size_t __i; dataType __t[dataCnt];} __s = {0, data}; x = __s.__t[__s.__i], __s.__i < dataCnt; __s.__i++)
#define with(x, ...) withInternal(__typeof(__VA_ARGS__), __typeof(__VA_ARGS__), x, PP_NARG(__VA_ARGS__), __VA_ARGS__)
#define withConst(x, ...) withInternal(__typeof(__VA_ARGS__), __typeof((__VA_ARGS__) + 0), x, PP_NARG(__VA_ARGS__), __VA_ARGS__)
#define withType(tn, x, ...) withInternal(tn, tn, x, PP_NARG(__VA_ARGS__), __VA_ARGS__)

当你使用它时:

int main()
{
int x;
int s1=2, s2=3;
with(x, in(s1, s2))
cout<<x<<endl;
withConst(x, in(45, 55))
cout<<x<<endl;
withType(int, x, in(45, s1, 55, s2))
cout<<x<<endl;
return 0;
}

我相信它更清楚。

  • with使用变量(基本上),因为当将常量传递给with时,gcc__typeof会为x生成一个无法分配的自动常量类型
  • withConst使用x+0技巧来删除变量的常量类型,但+运算符并不适用于所有数据类型,因此它有局限性
  • withType指定了一种数据类型,适用于混合情况

我想你可以试着像这样把东西批处理在一起:

while(1)
{
vector<int> things;
int rightThing = arrayRight[cursor];
things.push_back(rightThing);
int leftThing = arrayLeft[cursor];
things.push_back(leftThing);
for(vector<int>::iterator thing = things.begin(); thing != things.end(); ++thing)
{
// Process Thing
// ...
// ...
// ...
}
cursor++;
}

相关内容

最新更新