这是为什么常量常量常量声明中无效 [英] Why is this const const const declaration valid

查看:167
本文介绍了这是为什么常量常量常量声明中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个朋友问我解释为什么

 常量常量常量常量const int的常量I = 0;

是有效的语法。我拒绝对这个问题的看法。虽然我很好奇,如果它只是一个语法的事情吗?

编辑。标签是C ++,我的朋友被引用的gcc,所以我也许应该补充的C标记。


解决方案

从n1256:

6.7.3 / 4


  

如果同一合格网络连接器出现在同一不止一次
  特定网络连接ER-合格音响器列表,无论是直接或经由一个或多个的typedef,
  的行为是相同的,就好像它只出现一次。


在C ++中,这应该给一个错误。

这是C ++标准的03:

7.1.5 / 1 [dcl.type]


  

作为一般规则,至多一个类型说明符中允许的
  声明的完整DECL说明符 - 起。唯一的例外
  此规则有以下几种:


  
  

- const或挥发性可与任何其它类型说明符进行组合。不过,多余的CV-预选赛被禁止时,通过使用类型定义(7.1.3)或模板类型参数(14.3),在这种情况下,多余的cv修饰符会被忽略,除了介绍


从N3797(C ++ 11):

7.1.6 / 2 [dcl.type]


  

作为一般规则,至多一个类型特定网络连接器被允许在
  声明的或以类型特定网络ER-SEQ完整DECL-Fi无线特定ER-SEQ
  或尾随型特定网络ER-SEQ。唯一的例外是
  以下内容:

   - 常量可以用除了本身的任何类型说明符相结合。


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.

解决方案

From n1256:

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.

In C++, this should give an error.

From C++03 standard:

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. The only exceptions to this rule are the following:

— const or volatile can be combined with any other type-specifier. 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.

From N3797 (C++11):

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. The only exceptions to this rule are the following:

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

这篇关于这是为什么常量常量常量声明中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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