在 C 中允许重复的 const 限定符但在 C++ 中不允许?

2022-01-23 00:00:00 c language-agnostic constants 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.

相关文章