简体   繁体   English

逗号运算符做什么?

[英]What does the comma operator do?

What does the following code do in C/C++? 以下代码在C / C ++中做什么?

if (blah(), 5) {
    //do something
}

Comma operator is applied and the value 5 is used to determine the conditional's true/false. 应用逗号运算符,值5用于确定条件的真/假。

It will execute blah() and get something back (presumably), then the comma operator is employed and 5 will be the only thing that is used to determine the true/false value for the expression. 它将执行blah()并取回(大概),然后使用逗号运算符,并且5将是唯一用于确定表达式的真/假值的东西。


Note that the , operator could be overloaded for the return type of the blah() function (which wasn't specified), making the result non-obvious. 请注意,对于blah()函数的返回类型(未指定),操作符可能会重载,从而使结果不明显。

If the comma operator is not overloaded, the code is similar to this: 如果逗号运算符未重载,则代码类似于以下内容:

blah();
if (5) {
  // do something
}

If the comma operator is overloaded, the result will be based on that function. 如果逗号运算符重载,则结果将基于该函数。

#include <iostream>
#include <string>

using namespace std;

string blah()
{
    return "blah";
}

bool operator,(const string& key, const int& val) {
    return false;
}

int main (int argc, char * const argv[]) {

    if (blah(), 5) {
        cout << "if block";
    } else {
        cout << "else block";
    }

    return 0;
}

(edited to show comma operator overloading scenario. thanks to David Pierre for commenting on this) (已编辑,以显示逗号运算符重载的情况。感谢David Pierre对此发表评论)

I know one thing that this kind of code should do: it should get the coder fired. 我知道这种代码应该做的一件事:它应该使编码器被解雇。 I would be quite a bit afraid to work next to someone who writes like this. 我会非常害怕和这样的人一起工作。

In the pathological case, it depends on what the comma operator does... 在病理情况下,这取决于逗号运算符的作用。

class PlaceHolder
{
};

PlaceHolder Blah() { return PlaceHolder(); }

bool operator,(PlaceHolder, int) { return false; }

if (Blah(), 5)
{
    cout << "This will never run.";
}

我会说这取决于blah()。

On a more broad answer. 关于更广泛的答案。 The comma operator (non overloaded) resolves as in, execute the first part and return the second part. 逗号运算符(非重载)按原样解析,执行第一部分,然后返回第二部分。

So if you have (foo(),bar()) Both functions will be executed, but the value of the expression evaluates to bar() (and the type of the expression as well). 因此,如果您拥有(foo(),bar())这两个函数都将被执行,但是表达式的值将等于bar()(以及表达式的类型)。

While I won't say there are fair usages for that, is usually considered a bit hard to read code. 虽然我不会说有合理的用法,但通常认为它很难阅读代码。 Mainly because not many languages shares such constructs. 主要是因为没有多少语言共享这样的结构。 So As a personal rule of thumb I avoid it unless I am adding code to a preexistent expression and don't want to change completely its format. 因此,根据我个人的经验,除非我将代码添加到预先存在的表达式中并且不想完全更改其格式,否则我将避免使用它。

Example: I have a Macro (not discussing if you should use macros or not, sometimes its not even you that wrote it) 示例:我有一个宏(不讨论是否应使用宏,有时甚至不写宏)

FIND_SOMETHING(X) (x>2) ? FIND_SOMETHING(X)(x> 2)吗? find_fruits(x) : find_houses(x) find_fruits(x):find_houses(x)

And I usually use it in assignments like my_possession = FIND_SOMETHING(34); 而且我通常在诸如my_possession = FIND_SOMETHING(34);

Now I want to add log to it for debuggin purposes but I cannot change the find functions,. 现在,我想将日志添加到其中以进行调试,但是我无法更改查找功能。 I could do : 我可以做 :

FIND_SOMETHING(X) (x>2)? FIND_SOMETHING(X)(x> 2)? (LOG("looking for fruits"),find_fruits(x)):(LOG("looking for houses"),find_houses(x)) (LOG(“寻找水果”),find_fruits(x)):( LOG(“寻找房子”),find_houses(x))

The following was written assuming it is C code, either in a C file or within a C block of a C++ file: 编写以下代码时假定它是C代码,无论是在C文件中还是在C ++文件的C块中:

It is a pointless if . 如果没有意义。 It will call blah(), however the result of blah() is not considered by if at all. 它将调用胡说(),等等。然而()的结果通过,如果在所有不考虑。 The only thing being considered is 5, thus the if will always evaluate to true. 唯一要考虑的是5,因此if将始终评估为true。 IOW you could write this code as IOW,您可以将这段代码编写为

blah();
// do something

without any if at all. 没有任何如果在所有。

I use sometimes constructs like this for debugging purposes. 我有时使用类似这样的构造进行调试。 When I force the if close to be true regardless of the return value of blah. 当我强制if close为true时,无论blah的返回值如何。 It's obvious that it should never appear in production code. 显然,它永远不会出现在生产代码中。

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

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