gpt4 book ai didi

c++ - 在 C++ 中评估 block 语句的结果

转载 作者:行者123 更新时间:2023-11-30 03:19:42 24 4
gpt4 key购买 nike

我有一个声明变量“a”的最简单的 C++ 语句:

int a = ({ int b = 10; b; });

正如最近的 gcc 和 clang 所说,这是一个有效的语句,它真正声明了变量 a 的值为 10。

问题是:这是什么?我知道各种类型的表达方式。我知道各种类型的陈述。但是我在 C++ 14 标准中找不到任何提到“作为表达式的 block 语句返回最新的内部评估表达式”或类似的东西。

有人可以指出代码行完全有效的标准的确切行吗?

最佳答案

The question is: what is this?

这是对 ISO 标准 C 的 GNU 扩展,该扩展也可用于 C++,但同样不是 ISO C++ 的一部分。

引用 GCC Manual: Chapter 6 - Extensions to the C Language Family :

...

These extensions are available in C and Objective-C. Most of them are also available in C++. ...

  • Statement Exprs: Putting statements and declarations inside expressions.

后者在 GCC Manual: Section 6.1 - Statements and Declarations in Expressions 中有详细解释。 :

A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.

Recall that a compound statement is a sequence of statements surrounded by braces; in this construct, parentheses go around the braces. For example:

({ int y = foo (); int z;
if (y > 0) z = y;
else z = - y;
z; })

is a valid (though slightly more complex than necessary) expression for the absolute value of foo ().

...

至于 Clang,Clang Language Extensions描述了 Clang 旨在支持许多 GCC 扩展:[emhpasis 我的]:

This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.

关于c++ - 在 C++ 中评估 block 语句的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53457490/

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