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

C++ : Factorize code without passing a million arguments

我正在做 C/C++ 编程(主要是 C++),我发现自己需要分解实际上两倍相同的代码,除了每次出现的 "left" 都被替换为 "right"。代码完成后,我需要知道正在执行的是 "left" 还是 "right" 版本,仅此而已,两者都会 return 一个我可以理解的数字(一次结合左侧或右侧信息)。

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

所以我可以简单地通过将 left/right 替换为 "other" 来分解并调用分解函数两次,每次都知道我是为 "left" 还是为 [=24= 调用它].

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

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

有什么建议可以顺利进行因式分解吗?

    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 技巧来删除 const 类型的变量,但 + 运算符不适用于所有数据类型,因此它有局限性。
  • 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++;
}