Duplicate const qualifier allowed in C but not in C++?(在 C 中允许重复的 const 限定符但在 C++ 中不允许?)
问题描述
示例代码片段
const const const int x = 10;
int main()
{}
在 C 中编译,但不在 C++ 中.为什么它会在 C 中编译?我认为这在 C 中也会失败.没关系.
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.
C++ 标准的哪一部分禁止使用重复的 const
,而 C 标准的哪一部分允许这样做?
Which part of the C++ Standard forbids the use of duplicate const
and which part of the C standard allows this?
推荐答案
C99 §6.7.3/4:
C99 §6.7.3/4:
如果同一个限定符在同一个 specifier-qualifier-list 中出现多次,无论是直接出现还是通过一个或多个 typedef 出现,行为与如果它只出现一次.
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.
是的,那是有效的 C99,你的发现是正确的.
Yes, that is valid C99, and your discovery is correct.
这篇关于在 C 中允许重复的 const 限定符但在 C++ 中不允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C 中允许重复的 const 限定符但在 C++ 中不允许?


基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16