Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From n1256:

6.7.3/4

If the same quali?er appears more than once in the same speci?er-quali?er-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-speci?er is allowed in the complete decl-speci?er-seq of a declaration or in a type-speci?er-seq or trailing-type-speci?er-seq. The only exceptions to this rule are the following:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...