我正在进行C/C++编程(主要是C++),我发现自己需要分解代码,这些代码实际上是原来的两倍,除非每次出现的“左”都被替换为“右”。一旦代码完成,我需要知道它是正在执行的“左”或“右”版本,但仅此而已,它们都将返回一个我可以理解的数字(一旦与左或右信息组合在一起)。
在这个设置中,每个更改都需要进行两次,这是很烦人的。
因此,我可以简单地用"other“替换左/右,然后调用两次分解函数,每次都知道我是为”左“还是”右“调用它。
现在,当我们到达代码的那一部分时,已经有了一百万个变量(游标、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++;
}
发布于 2016-06-13 00:20:05
尝尝这个?(它只在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
使用变量(基本上),因为当将consts传递给with
时,gcc __typeof
为x
生成一个无法分配的自动const类型。withConst
使用x+0
技巧来删除const类型的变量,但是+
操作符并不适用于每一种数据类型,因此它有其局限性。withType
指定一种数据类型,它适用于混合情况。发布于 2016-06-13 00:26:51
我想你可以试着把这些东西分批到一起:
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++;
}
https://stackoverflow.com/questions/37784640
复制