简体   繁体   English

为什么这个const const const声明有效

[英]Why is this const const const declaration valid

A friend asked me to explain why 一位朋友让我解释原因

const const const const const int const i = 0;

is valid syntax. 是有效的语法。 I declined to have any thoughts on the subject. 我拒绝对这个问题有任何想法。 Though I'm curious if it is just a grammar thing? 虽然我很好奇它是否只是一个语法的东西?

Edit. 编辑。 Tag is C++, my friend was referencing gcc, so I should probably add the C tag. 标签是C ++,我的朋友正在引用gcc,所以我应该添加C标签。

From n1256: 从n1256:

6.7.3/4 6.7.3 / 4

If the same qualifier appears more than once in the same specifier-qualifier-list,either directly or via one or more typedefs, the behavior is the same as if it appeared only once. 如果相同的限定符在同一个特定的限定符列表中出现不止一次,无论是直接还是通过一个或多个typedef,行为都与它只出现一次相同。

In C++, this should give an error. 在C ++中,这应该会出错。

From C++03 standard: 从C ++ 03标准:

7.1.5/1 [dcl.type] 7.1.5 / 1 [dcl.type]

As a general rule, at most one type-specifier is allowed in the complete decl-specifier-seq of a declaration. 作为一般规则,在声明的完整decl-specifier-seq中最多允许一个类型说明符。 The only exceptions to this rule are the following: 此规则的唯一例外情况如下:

— const or volatile can be combined with any other type-specifier. - const或volatile可以与任何其他类型说明符组合使用。 However, redundant cv- qualifiers are prohibited except when introduced through the use of typedefs (7.1.3) or template type arguments (14.3), in which case the redundant cv-qualifiers are ignored. 但是,除非通过使用typedef(7.1.3)或模板类型参数(14.3)引入冗余cv限定符,否则将禁止冗余cv限定符,在这种情况下,将忽略冗余cv限定符。

From N3797 (C++11): 从N3797(C ++ 11):

7.1.6/2 [dcl.type] 7.1.6 / 2 [dcl.type]

As a general rule, at most one type-specifier is allowed in the complete decl-specifier-seq of a declaration or in a type-specifier-seq or trailing-type-specifier-seq. 作为一般规则,在声明的完整decl-speci-seq或类型-peci-seq或trailing-type-speci-seq中,最多只允许一个类型指定符。 The only exceptions to this rule are the following: 此规则的唯一例外情况如下:

 — const can be combined with any type specifier except itself. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM