是C ++的“声明和初始化”吗?陈述,表达? [英] Is C++ "declaration and initialization" statement, an expression?

查看:75
本文介绍了是C ++的“声明和初始化”吗?陈述,表达?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语言标准说:


[注意:第5章定义了语法,求值顺序和含义, expressions.58表达式是运算符和
操作数的序列,用于指定计算。表达式可能会导致
值,并可能导致副作用。 —尾注]

[ Note: Clause 5 defines the syntax, order of evaluation, and meaning of expressions.58 An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects. — end note ]

例如我的代码如下:

int i=1;
A obj;

那么,以上两个语句是否都算作表达式?

So, do both statements above, count as "expression"?

stackoverflow上的某些人说 int i = 1;不是表达。这对我来说很奇怪。

Some people on stackoverflow says "int i=1;" is not an expression. This is quite odd to me.

(1)初始化是一种计算,对吗?所以应该认为它是表达式?

(1) An initialization is a kind of "computation", right? So it should be considered as "expression"?

(2)A obj; //调用ctor。 ctor是一种计算,因此应将其视为表达式?

(2) A obj; //invokes a ctor. A ctor is kind of computation, so it should be considered as "expression"?

推荐答案

该标准中的非规范性注释旨在激发表达式的概念,但不是实际的定义。 expression 的定义在第5节其余部分给出的语言语法中给出。表达式由某些终端(例如,文字,变量名称和函数名称)构建而成,它们组合在一起

That non-normative note in the standard is intended to motivate the concept of an expression, but is not the actual definition. The definition of expression is given in the language grammar which is given in the remainder of clause 5. Expressions are built out of certain terminals such as literals, names of variables, and names of functions, which are combined using operators such as arithmetic and relational operators.

声明和表达式是不同的语法实体,因此在C ++程序中找到的声明绝不是表达式,并且 vice反之亦然。乍一看,差异是很容易的:如果声明了某些内容,那就是声明。

Declarations and expressions are distinct syntactic entities, so a declaration found inside a C++ program is never an expression, and vice versa. The difference is fairly easy to see at a glance: if it declares something, it's a declaration.

1;          // expression statement
int i = 1;  // declaration statement that declares `i`
A(i, 42);   // expression statement that creates an A object
A a(i);     // declaration statement that declares an A object (named a)

声明可以求值表达式,但声明不是表达式。您正确地指出,类类型对象的声明会导致构造函数调用。

A declaration can evaluate expressions but a declaration is not an expression. You rightly point out that the declaration of an object of class type can cause a constructor call. Still it is syntactically a declaration and not an expression.

但是,从另一个意义上说,声明。即,关于表达式中求值顺序的规则也适用于声明。例如,有一条规则规定,后缀增量对 int 的副作用发生在完全表达式结束之前的某个时刻。

However, there is another sense in which a declaration is an expression. Namely, rules about the sequencing of evaluations within expressions also apply to declarations. For example, there is a rule that the side effect of a postfix increment on an int takes place at some point before the end of the full-expression.

f(i++) + g();  // i may be incremented before or after g() is called...
h();           // but definitely before h() is called.

出于此类规则的目的,单个变量的声明和初始化也被视为充分表达。对于类类型的变量,构造函数调用是该表达式的一部分。

For the purposes of such rules, the declaration and initialization of a single variable is considered to also be a full-expression. In the case of a variable of class type, the constructor call is part of that expression.

int i = 1;                      // this declaration evaluates a full-expression
                                // whose effect is to initialize `i` to 1
int j = f(i++) + g(), k = h();  // two separate full-expressions;
                                // i is incremented before h() is called

之前,i会递增需要弄清楚上下文以便弄清楚表达是什么意思。

When reading the standard, you need to consider the context in order to figure out what sense of "expression" is meant.

这篇关于是C ++的“声明和初始化”吗?陈述,表达?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆