C/C++后加1以上
我正在从缓冲区读取字节。但有时我读的只是一个词或更长。
// assume buffer is of type unsigned char *
read_ptr(buffer+(position++))
没问题,但是我怎样才能将位置后增加 2 或 4 呢?我无法让 +=
运算符进行后递增,是吗?
原因是,我有一个大而可怕的表达式,我想对其进行评估,同时递增位置变量。
我想我想出了自己的解决方案。我很确定它有效。但每个人都会讨厌它,因为这不是非常可读的代码。
read_ptr(buffer+(position+=4)-4)
在对其进行一些测试以确保它做正确的事情之后,我将把它变成一个宏。
结论:
不要这样做。这只是一个坏主意,因为这会生成无法维护的代码。但是......事实证明,将任何预递增运算符转换为后递增运算符确实非常容易。
I'm reading bytes from a buffer. But sometimes what I'm reading is a word or longer.
// assume buffer is of type unsigned char *
read_ptr(buffer+(position++))
That's fine but how can I post-increment position by 2 or 4? There's no way I can get the +=
operator to post-increment, is there?
Reason is, I have this big awful expression which I want to evaluate, while at the same time incrementing the position variable.
I think I came up with my own solution. I'm pretty sure it works. Everyone's gonna hate it though, since this isn't very readable code.
read_ptr(buffer+(position+=4)-4)
I will then make this into a macro after testing it a bit to make sure it's doing the right thing.
IN CONCLUSION:
Don't do this. It's just a bad idea because this is the sort of thing that generates unmaintainable code. But... it does turn out to be quite easy to convert any pre-incrementing operator into a post-incrementing one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您不能将变量后递增 2 或 4,但您可以使用以下内容(根据您的情况)
read_ptr(buffer+position);位置 += 2;
You can't post-increment a variable by 2 or 4 but you can use the following (in your case)
read_ptr(buffer+position); position += 2;
虽然,我不推荐这个解决方案,但如果您不想更改代码中的这一行:
并且您仍然想将
position
后置 2,则定义 position< /em> asIndex position(2);
其中类型Index
在此定义,并且还显示了用法:输出:
工作示例:http://ideone.com/CFgal
注意: 我仍然不会在现实项目中建议这种解决方案。这更像是拼图 :D
Although, I would not recommend this solution, but if you don't want to change this line in your code:
And you still want to post-increment
position
by 2, then define position asIndex position(2);
where the typeIndex
is defined here, and also shown the usage:Output:
Working Example : http://ideone.com/CFgal
Note: I would still not suggest this solution in real life project. It's more like puzzle :D
+= 运算符将是一个单独的语句(不是后增量或预增量)。您可以使用以下行:
The += operator would be a separate statement (not post or pre increment). You could use the following line:
你不知道;你把它分成多行。没有理由将所有内容都塞到这里的一行中。
You don't; you break it up into more than one line. There is no reason to stuff everything into one line here.
好吧,我确实在编辑中回答了我的问题...基本上我想要的是一个单个表达式,它计算原始值,但具有任意增量的副作用。这是一些宏。
Well, I did answer my question in the edit... Basically what I wanted was a single expression which evaluates to the original value but has a side effect of incrementing by an arbitrary amount. Here are some macros.
如果
position
是指向int16
或int32
的指针,则递增它会分别加 2 或 4。If
position
were a pointer toint16
orint32
, incrementing it would add 2 or 4, respectively.在 C++ 中,您可以轻松编写一个函数来执行后置式双增量:
在 C 中,情况稍微尴尬一些:
您也可以通过将其作为附加函数参数或在C++案例。
还有第二个问题,是否值得在 C++ 或 C 中追求这种编程风格,在 C 中,您可以在一条语句中完成这么多工作。避免副作用可以使代码更容易理解,即使代码更长。
In C++, you can easily write a function to perform a post-style double-increment:
In C it's slightly more awkward:
You can cover the 4 case as well by making it an additional function parameter, or perhaps an additional template parameter in the C++ case.
There's a second question, whether it's worth pursuing this style of programming in C++ or in C, where you do so much in a single statement. Avoiding side-effects can make the code easier to understand, even though it comes out longer.
Steve Jessop 的答案的稍微更通用的版本接受所有类型的增量步骤。
如果您明确需要调用
operator++
而不是operator+=
:应用程序:
A slightly more generalized version of Steve Jessop's answer to accept all kind of increment steps.
If you explicity need to call
operator++
instead ofoperator+=
:Application :