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

前端 未结 4 445
不知归路
不知归路 2020-12-03 13:35

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 thou

4条回答
  •  抹茶落季
    2020-12-03 14:24

    The C++0x grammar appears to allow it:

    cv-qualifier-seq:

    • cv-qualifier cv-qualifier-seq opt

    cv-qualifier:

      const
    
      volatile
    

    Also, [decl.type.cv] appears to allow it:

    There are two cv-qualifiers, const and volatile. If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list of the declaration shall not be empty. [ Note: 3.9.3 and 8.3.5 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 ]

提交回复
热议问题