gpt4 book ai didi

c++ - 逗号分隔变量定义中的先序关系

转载 作者:行者123 更新时间:2023-11-30 03:54:04 25 4
gpt4 key购买 nike

让我们从以下示例代码开始:

int a = 0, b = a++, c = a;

a++ 是否在 a 之前(在 c = a 内)? a++a 似乎符合完整表达式的条件,并且根据 cppreference (规则 1),答案应该是肯定的。但我不确定。

最佳答案

是的。作为Brian指出,这不是逗号运算符,而是一个init-declarator-list。从 [dcl.decl] 我们有:

Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.

用脚注阐明:

A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is

T D1, D2, ... Dn;

is usually equivalent to

T D1; T D2; ... T Dn;

where T is a decl-specifier-seq and each Di is an init-declarator.

有两个异常(exception),一个是隐藏类型的名称,一个是auto,两者都不适用。所以最终,您拥有的代码完全等同于:

int a = 0;
int b = a++;
int c = a;

您应该首先写下它,因为它不需要搜索标准来确保您做的事情是有效的!

关于c++ - 逗号分隔变量定义中的先序关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29761114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com