Reputation: 1279
Having two const
's for a type issues a warning / error. However if the type has been defined with typedef
, the compiler accepts it (both Visual Studio 2013 and the online compiler C++ shell).
#include <iostream>
typedef const int value_type;
int main()
{
const value_type n = 0; //ok
const const int n2 = 0; //error C4114
return 0;
}
Does anyone have an idea as to why? Is it that one is const (const int)
, which is different from const const int
?
Upvotes: 24
Views: 1057
Reputation: 2249
Last change to get rid of is Solution wide Disable
In XML then looks like
...
<DisableSpecificWarnings>4114</DisableSpecificWarnings>
</ClCompile>
or line parameter shows /wd"4114"
Upvotes: 0
Reputation: 170153
It's explicitly allowed in the typedef case, and disallowed in the declaration itself:
The type-specifiers are:
type-specifier : ... cv-qualifier
defining-type-specifier : type-specifier
The specifiers that can be used in a declaration are:
decl-specifier : ... defining-type-specifier ...
Each decl-specifier shall appear at most once in a complete decl-specifier-seq, except that long may appear twice.
There are two cv-qualifiers, const and volatile. Each cv-qualifier shall appear at most once in a cv-qualifier-seq. If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list or member-declarator-list of the declaration shall not be empty. [ Note: [basic.type.qualifier] and [dcl.fct] describe how cv-qualifiers affect object and function types. — end note ] Redundant cv-qualifications are ignored. [ Note: For example, these could be introduced by typedefs. — end note ]
Besides type aliases, a template parameter is another case where the qualifier could be redundant. The rationale for allowing this, is to not break otherwise correct declarations just because a cv-qualifier snuck in the back door.
Upvotes: 31