C/C++语法 - 用 , 而不是 ; 分隔语句合法的?

发布于 2024-12-15 17:00:04 字数 174 浏览 1 评论 0原文

我刚刚遇到了这段代码:

delete a, a = 0;

它编译并运行得很好。但这不应该是:

delete a;
a = 0;

为什么在这种情况下允许使用 , 分隔语句?

谢谢 :)

I just ran into this piece of code that does this :

delete a, a = 0;

It compiles and runs just fine. But isn't this supposed to be :

delete a;
a = 0;

Why is separating statements using , allowed in this case ?

Thanks :)

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

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

发布评论

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

评论(5

末骤雨初歇 2024-12-22 17:00:04

在 C 和 C++ 中,大多数“语句”实际上是表达式。添加到表达式中的分号使其成为语句。或者,允许(但几乎总是不好的风格)用逗号运算符分隔有副作用的表达式:评估左侧表达式的副作用(并且其值被丢弃),而右侧表达式被评估其副作用(并且其值被丢弃)。计算 -side 表达式的值。

In C and C++, most "statements" are actually expressions. The semicolon added to an expression makes it into a statement. Alternatively, it is allowed (but almost always bad style) to separate side-effectful expressions with the comma operator: the left-hand-side expression is evaluated for its side-effects (and its value is discarded), and the right-hand-side expression is evaluated for its value.

爱给你人给你 2024-12-22 17:00:04

这是逗号运算符。它评估两个参数并返回第二个参数。

This is the comma-operator. It evaluates both it's arguments and returns the second one.

木緿 2024-12-22 17:00:04

这是逗号运算符。它可用于分隔表达式,但不能用于分隔声明。

This is the comma operator. It can be used to separate expressions, but not declarations.

提赋 2024-12-22 17:00:04

逗号运算符。 MSDN 文章位于此处。看看这个问题来了解它是如何工作的。

That is comma operator. MSDN article is here. And have a look at this question to understand how it works.

失与倦" 2024-12-22 17:00:04

虽然可以编写这样的代码,但可能有点奇怪。一个稍微更实际的用例是,如果您有一个 struct T ,如下所示:

struct T {
    bool check() const;
    void fix();
};

现在您想要迭代该结构中的所有内容并对其运行检查,然后在检查返回 false 时调用修复。执行此操作的简单方法是

for (list<T>::iterator it = mylist.begin(); it < mylist.end(); ++it)
     if (!it->check())
         it->fix();

假设您想以尽可能短的方式编写它。 fix() 返回 void 意味着您不能只是将其放入条件中。但是,使用逗号运算符可以解决这个问题:

for (auto it = mylist.begin(); it != mylist.end() && (it->check() || (it->fix(), true)); ++it);

如果没有特别充分的理由,我不会使用它,但它确实允许您从条件调用任何函数,这很方便。

While it is possible to write code like that, it may be somewhat weird. A slightly more realistic usecase would be if you have a struct T as follows:

struct T {
    bool check() const;
    void fix();
};

Now you want to iterate through everything in the struct and run check on it, and then call fix if check returns false. The simple way to do this would be

for (list<T>::iterator it = mylist.begin(); it < mylist.end(); ++it)
     if (!it->check())
         it->fix();

Let's pretend you want to write it in as short a way as possible. fix() returning void means you can't just put it in the condition. However, using the comma operator you can get around this:

for (auto it = mylist.begin(); it != mylist.end() && (it->check() || (it->fix(), true)); ++it);

I wouldn't use it without a particularly good reason, but it does allow you to call any function from a condition, which can be convenient.

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