逗号运算符有什么作用?

发布于 2024-07-06 00:35:13 字数 89 浏览 7 评论 0原文

下面的代码在 C/C++ 中做什么?

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

What does the following code do in C/C++?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

任性一次 2024-07-13 00:35:13

如果逗号运算符未重载,则代码类似于以下内容:

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

如果逗号运算符重载,则结果将基于该函数。

#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;
}

(经过编辑以显示逗号运算符重载场景。感谢 David Pierre 对此发表评论)

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)

影子是时光的心 2024-07-13 00:35:13

应用逗号运算符并使用值 5 来确定条件的真/假。

它将执行 blah() 并返回一些内容(大概),然后使用逗号运算符,并且 5 将是唯一用于确定表达式的真/假值的东西。


请注意,可以针对 blah() 函数(未指定)的返回类型重载 , 运算符,从而使结果不明显。

Comma operator is applied and the value 5 is used to determine the conditional's true/false.

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.


Note that the , operator could be overloaded for the return type of the blah() function (which wasn't specified), making the result non-obvious.

-柠檬树下少年和吉他 2024-07-13 00:35:13

我知道这种代码应该做的一件事:它应该让编码员被解雇。 我会有点害怕和这样写文章的人一起工作。

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.

深爱不及久伴 2024-07-13 00:35:13

在病理情况下,这取决于逗号运算符的作用......

class PlaceHolder
{
};

PlaceHolder Blah() { return PlaceHolder(); }

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

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

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.";
}
痴情换悲伤 2024-07-13 00:35:13

我想说这取决于 blah()。

I would say that depends on blah().

疏忽 2024-07-13 00:35:13

关于更广泛的答案。 逗号运算符(非重载)解析为,执行第一部分并返回第二部分。

因此,如果 (foo(),bar()) 两个函数都将被执行,但表达式的值计算为 bar() (以及表达式的类型)。

虽然我不会说它有合理的用法,但通常被认为有点难以阅读代码。 主要是因为没有多少语言共享这样的结构。 因此,根据个人经验,我会避免使用它,除非我将代码添加到预先存在的表达式中并且不想完全更改其格式。

示例:我有一个宏(不讨论是否应该使用宏,有时甚至不是你写的)

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

我通常在像 my_possession = FIND_SOMETHING(34); 这样的作业中使用它

现在我想向其添加日志以用于调试目的,但我无法更改查找功能。 我可以这样做:

FIND_SOMETHING(X) (x>2)? (LOG("寻找水果"),find_fruits(x)):(LOG("寻找房子"),find_houses(x))

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

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_fruits(x) : find_houses(x)

And I usually use it in assignments like 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)? (LOG("looking for fruits"),find_fruits(x)):(LOG("looking for houses"),find_houses(x))

她说她爱他 2024-07-13 00:35:13

我有时会使用这样的结构来进行调试。 当我强制 if close 为 true 时,无论 blah 的返回值如何。
很明显,它永远不应该出现在生产代码中。

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.
It's obvious that it should never appear in production code.

子栖 2024-07-13 00:35:13

以下内容是假设它是 C 代码(在 C 文件中或在 C++ 文件的 C 块中)编写的:

这是毫无意义的 if。 它会调用 blah(),但是 if 根本不考虑 blah() 的结果。 唯一考虑的是 5,因此 if 将始终评估为 true。 IOW,您可以编写这段代码,根本

blah();
// do something

不需要任何 if

The following was written assuming it is C code, either in a C file or within a C block of a C++ file:

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. IOW you could write this code as

blah();
// do something

without any if at all.

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