条件语句中的逗号运算符

发布于 2024-10-13 10:33:11 字数 329 浏览 1 评论 0原文

我读过很多地方,但我真的无法理解条件中的指定行为。

我知道在赋值中它评估第一个操作数,丢弃结果,然后评估第二个操作数。

但是对于这段代码,它应该做什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱的十字路口 2024-10-20 10:33:11

在这种情况下,它是不必要的表达式,可以删除而不改变代码的含义。

In this case, it is an unnecessary expression, and can be deleted without changing the meaning of the code.

十年九夏 2024-10-20 10:33:11

逗号运算符执行第一项的表达式,丢弃结果,然后将结果计算为最后一个表达式。

因此 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 whatever partfile would, discard that result, then evaluate and return bDeleted

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

巷雨优美回忆 2024-10-20 10:33:11
bool bDeleted = false;
if (partfile,bDeleted)
    partfile->PerformFileCompleteEnd(wParam);

此处,if 语句计算partfile,bDeleted,但bDelete 始终为假,因此表达式无法运行。关键问题是“这到底是怎么回事?”。可能的答案是,有人暂时想要阻止 partfile->PerformFileCompleteEnd(wParam); 语句运行,可能是因为它导致了一些问题,或者他们想要确保以后的代码正确报告错误(如果出现这种情况)未执行步骤。为了让他们记住代码过去的样子,他们保留了旧的“if (partfile)”逻辑,但添加了一个硬编码的 bDeleted 变量来记录 partfile->Perform... 逻辑实际上已从程序中“删除”。

暂时禁用此类代码的更好方法可能是...

#if 0
    if (partfile)
        partfile->PerformFileCompleteEnd(wParam);
#endif

...尽管有时我也尝试记录推理...

#ifndef DONT_BYPASS_FILE_COMPLETE_PROCESSING_DURING_DEBUGGING
    if (partfile)
        partfile->PerformFileCompleteEnd(wParam);
#endif

...或者...

if (partFile, !"FIXME remove this after debugging")
    partfile->PerformFileCompleteEnd(wParam);

最佳选择取决于您的工具集和现有习惯(例如某些编辑器在反向视频中突出显示“FIXME”和“TODO”,这样就很难错过或灰显 #if 0 块;您可能有源代码控制签入警告的特定字符串;仅在调试版本和发布版本中定义可以防止意外分发等.)。

bool bDeleted = false;
if (partfile,bDeleted)
    partfile->PerformFileCompleteEnd(wParam);

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 the partfile->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 the partfile->Perform... logic had effectively been "deleted" from the program.

A better way to temporarily disable such code is probably...

#if 0
    if (partfile)
        partfile->PerformFileCompleteEnd(wParam);
#endif

...though sometimes I try to document the reasoning too...

#ifndef DONT_BYPASS_FILE_COMPLETE_PROCESSING_DURING_DEBUGGING
    if (partfile)
        partfile->PerformFileCompleteEnd(wParam);
#endif

...or...

if (partFile, !"FIXME remove this after debugging")
    partfile->PerformFileCompleteEnd(wParam);

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.).

抹茶夏天i‖ 2024-10-20 10:33:11

评估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.

不顾 2024-10-20 10:33:11

逗号运算符是 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文