简体   繁体   English

有条件的逗号运算符

[英]Comma operator in a conditional

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? IF中的partfile是一个不必要的参数,或者它有什么意义?

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

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 所以partfile,bDeleted将评估任何partfile ,丢弃该结果,然后评估并返回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 有关更多信息,请参阅Wikipedia:逗号运算符

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. 这里, if语句计算partfile,bDeleted,但bDelete始终为false,因此表达式无法运行。 The key question is "what's that all about?". 关键问题是“那是什么意思?”。 The probable answer is that someone temporarily wanted to prevent the partfile->PerformFileCompleteEnd(wParam); 可能的答案是有人暂时想要阻止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. 为了让他们记住代码的原理,他们在那里留下了旧的“if(partfile)”逻辑,但添加了一个硬编码的bDeleted变量来记录partfile->Perform...逻辑已经被“删除”了来自该计划。

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

partfile is evaluated, then bDeleted is evaluated and used as the test. 评估partfile,然后评估bDeleted并将其用作测试。 Since evaluation of partfile does not have any side effects, removing it from the conditional has no effect. 由于partfile的评估没有任何副作用,因此从条件中删除它不起作用。

The comma operator is a rather obscure feature of C/C++. 逗号运算符是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) ). 它不应该与初始化列表中的逗号混淆(即:int x,int y;),也不应与函数调用参数分离逗号(即: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. 对于C / C ++中的几乎每个运算符,表达式的求值顺序都是未定义的。 If I write 如果我写

result = x + y; result = x + y;

where x and y are subexpressions, then either x or y can be evaluated first. 其中x和y是子表达式,然后可以首先评估x或y。 I cannot know which, it's up to the compiler. 我不知道哪个,这取决于编译器。 If you however write 如果你写的话

result = x, y; 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... 当然,在现实世界的应用程序中使用它是非常有限的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM