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++ 中不允许?


基础教程推荐
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++,'if' 表达式中的变量声明 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01