C ++标准的哪一部分允许在括号中声明变量? [英] Which part of the C++ standard allow to declare variable in parenthesis?

查看:69
本文介绍了C ++标准的哪一部分允许在括号中声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

int main() {
    int(s);
}

我为它创建有效变量 s 。有人可以解释这里发生了什么吗?

I am surprised by the fact that it creates valid variable s. Can anyone explain what's happening here?

推荐答案

[dcl。意义]在标准中说:

[dcl.meaning] in the Standard says:


在声明 TD 中,其中 D 的格式为(D1)包含的 declarator-id 的类型与声明<$ c中包含的 declarator-id 的类型相同$ c> T D1 。

In a declaration T D where D has the form ( D1 ) the type of the contained declarator-id is the same as that of the contained declarator-id in the declaration T D1.

括号不会更改嵌入的 declarator-id 的类型,但可以更改绑定

Parentheses do not alter the type of the embedded declarator-id, but they can alter the binding of complex declarators.

更简单地说,您可以在任何被认为是声明符的地方加上括号。在C ++语法中。 (松散地说,声明符是声明的一部分,没有包含一个名称的初始说明符和类型。)

More simply, you can put parentheses around anything considered a "declarator" in the C++ grammar. (Loosely speaking, a declarator is a part of a declaration without the initial specifiers and types which contains one name.)

在您的示例中,标识符 s 是一个声明符,因此您可以在其周围加上括号,并且含义不会改变。

In your example, the identifier s is a declarator, so you're allowed to put parentheses around it and the meaning doesn't change.

第二个引用的句子暗示了这样做的原因,当事情变得更复杂时,这是必要的。一个例子:

The reason for this, as the second quoted sentence hints, is that it can be necessary when things get more complicated. One example:

int * a [10];     // a is an array of ten pointers to int.
int ( * b ) [10]; // b is a pointer to an array of ten ints.

这篇关于C ++标准的哪一部分允许在括号中声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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