条件语句中的逗号运算符
我读过很多地方,但我真的无法理解条件中的指定行为。
我知道在赋值中它评估第一个操作数,丢弃结果,然后评估第二个操作数。
但是对于这段代码,它应该做什么?
CPartFile* partfile = (CPartFile*)lParam;
ASSERT( partfile != NULL );
bool bDeleted = false;
if (partfile,bDeleted)
partfile->PerformFileCompleteEnd(wParam);
IF 中的部分文件是一个不必要的参数,或者它有什么意义吗?
I have read in a lot of places but I really can't understand the specified behavior in conditionals.
I understand that in assignments it evaluates the first operand, discards the result, then evaluates the second operand.
But for this code, what it supposed to do?
CPartFile* partfile = (CPartFile*)lParam;
ASSERT( partfile != NULL );
bool bDeleted = false;
if (partfile,bDeleted)
partfile->PerformFileCompleteEnd(wParam);
The partfile in the IF was an unnecessary argument, or it have any meaning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这种情况下,它是不必要的表达式,可以删除而不改变代码的含义。
In this case, it is an unnecessary expression, and can be deleted without changing the meaning of the code.
逗号运算符执行第一项的表达式,丢弃结果,然后将结果计算为最后一个表达式。
因此
partfile,bDeleted
会评估partfile
会评估的任何内容,丢弃该结果,然后评估并返回bDeleted
如果您需要评估具有副作用(例如,调用方法)。但在这种情况下,它是没有用的。
有关详细信息,请参阅维基百科:逗号运算符
The comma operator performs the expression of the first item, discards the results, then evaluates the result as the last expression.
So
partfile,bDeleted
would evaulate whateverpartfile
would, discard that result, then evaluate and returnbDeleted
It's useful if you need to evaluate something which has a side-effect (for example, calling a method). In this case, though, it's useless.
For more information, see Wikipedia: Comma operator
此处,
if
语句计算partfile,bDeleted,但bDelete 始终为假,因此表达式无法运行。关键问题是“这到底是怎么回事?”。可能的答案是,有人暂时想要阻止partfile->PerformFileCompleteEnd(wParam);
语句运行,可能是因为它导致了一些问题,或者他们想要确保以后的代码正确报告错误(如果出现这种情况)未执行步骤。为了让他们记住代码过去的样子,他们保留了旧的“if (partfile)”逻辑,但添加了一个硬编码的 bDeleted 变量来记录partfile->Perform...
逻辑实际上已从程序中“删除”。暂时禁用此类代码的更好方法可能是...
...尽管有时我也尝试记录推理...
...或者...
最佳选择取决于您的工具集和现有习惯(例如某些编辑器在反向视频中突出显示“FIXME”和“TODO”,这样就很难错过或灰显 #if 0 块;您可能有源代码控制签入警告的特定字符串;仅在调试版本和发布版本中定义可以防止意外分发等.)。
Here, the
if
statement evaluates partfile,bDeleted, but bDelete is always false, so the expression fails to run. The key question is "what's that all about?". The probable answer is that someone temporarily wanted to prevent thepartfile->PerformFileCompleteEnd(wParam);
statement from running, perhaps because it was causing some problem or they wanted to ensure later code reported errors properly if that step wasn't performed. So that they're remember how the code used to be, they left the old "if (partfile)" logic there, but added a hardcoded bDeleted variable to document that thepartfile->Perform...
logic had effectively been "deleted" from the program.A better way to temporarily disable such code is probably...
...though sometimes I try to document the reasoning too...
...or...
The best choice depends on your tool set and existing habits (e.g. some editors highlight "FIXME" and "TODO" in reverse video so it's hard to miss or grey out #if 0 blocks; you might have particular strings your source-control checkin warns about; preprocessor defines only in debug vs release builds can prevent accidental distribution etc.).
评估partfile,然后评估bDeleted 并用作测试。由于对partfile的评估没有任何副作用,因此从条件中删除它没有任何影响。
partfile is evaluated, then bDeleted is evaluated and used as the test. Since evaluation of partfile does not have any side effects, removing it from the conditional has no effect.
逗号运算符是 C/C++ 中一个相当晦涩的功能。它不应与初始化列表中的逗号(即: int x, int y; )或函数调用参数分隔逗号(即: func(x, y) )混淆。
逗号运算符只有一个目的:为程序员提供有保证的表达式求值顺序。对于 C/C++ 中的几乎每个运算符,表达式的求值顺序都是未定义的。如果我写
result = x + y;
其中 x 和 y 是子表达式,则可以首先计算 x 或 y。我不知道哪个,这取决于编译器。但是如果你写
result = x, y;
评估顺序由标准保证:左先。
当然,它在现实世界的应用中的用途是相当有限的......
The comma operator is a rather obscure feature of C/C++. It should not be confused with the comma in initialising lists (ie: int x, int y; ) nor with function call parameter separation comma (ie: func(x, y) ).
The comma operator has one single purpose: to give the programmer a guaranteed order of evaluation of an expression. For almost every operator in C/C++, the order of evaluation of expressions is undefined. If I write
result = x + y;
where x and y are subexpressions, then either x or y can be evaluated first. I cannot know which, it's up to the compiler. If you however write
result = x, y;
the order of evaluation is guaranteed by the standard: left first.
Of course, the uses of this in real world applications are quite limited...