Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate const qualifier allowed in C but not in C++?

Tags:

Sample code snippet

const const const int x = 10;   
int main()
{}

gets compiled in C but not in C++. Why does it get compiled in C? I thought this would fail in C as well. Never mind.

Which part of the C++ Standard forbids the use of duplicate const and which part of the C standard allows this?

like image 788
Prasoon Saurav Avatar asked Apr 25 '11 17:04

Prasoon Saurav


2 Answers

C99 §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 typedef s, the behavior is the same as if it appeared only once.

Yes, that is valid C99, and your discovery is correct.

like image 138
Potatoswatter Avatar answered Jan 04 '23 00:01

Potatoswatter


From the last C++0x draft, [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.

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

— signed or unsigned can be combined with char, long, short, or int.

— short or long can be combined with int.

— long can be combined with double.

— long can be combined with long.

like image 32
Yakov Galka Avatar answered Jan 04 '23 00:01

Yakov Galka