C 和 C++ 标准之间的关系是什么?

What is the Relationship Between the C and C++ Standards?

我在写this answer and I quoted from http://en.cppreference.com/w/cpp/string/byte/tolower#Parameters

Is not representable as unsigned char and does not equal EOF, the behavior is undefined

我去考察的时候the edit that had added this phrase发现作者的评论:

Can't use negative signed chars with any ctype.h function per C99 7.4/1

作者引用的是 C++ 文档中的 C99 标准。那有效吗?我在 C++ 标准中找不到关于此函数定义的任何内容,因此我必须假设它是有效的。

但这让我担心有两个原因:

  1. 我如何知道 C++ 标准所依赖的 C 标准版本?
  2. the discrepancies between C and C++ everywhere 个列表。如果我参考 C++ 查看 C 标准,我怎么可能知道我正在查看的区域是否已被覆盖?

第一个问题:

C++ 标准在其 规范性参考文献 部分中明确列出了它所依赖的 C 标准。对于 C++14,[intro.refs] 1.2/1 恰好列出 C 99:

  • ISO/IEC 9899:1999, Programming languages — C
  • ISO/IEC 9899:1999/Cor.1:2001(E), Programming languages — C, Technical Corrigendum 1
  • ISO/IEC 9899:1999/Cor.2:2004(E), Programming languages — C, Technical Corrigendum 2
  • ISO/IEC 9899:1999/Cor.3:2007(E), Programming languages — C, Technical Corrigendum 3

第二个问题:

C++ 标准并未隐式包含 C 标准的 任何 部分;所有对 C 标准的引用都是明确的。关于 C++ 与 C 的不同之处的一个很好的信息来源是附件 C,C++ 标准的 "Compatibility",特别是 C.1 [diff.iso].

此外,对 C 标准库的引用分散在整个 C++ 标准库的描述中(C++14 中的第 17–30 章)。特别感兴趣的可以是:

  • 17.2 [library.c],描述了C标准库的基本包含
  • 第 18 章 [[=​​36=]],描述了 C++ 标准库的许多 <c:::> 头文件(提供 C 标准库功能的头文件)。

How would I know what version of the C standard the C++ standard depends upon?

在 C++ 14 中,如 N4140 中的 1.2 [intro.refs] 所述,它是 ISO/IEC 9899:1999(加上三个更正,因此本质上是 C99)。在 C++98 中,它是 C90,在 C++17 中,它可能是 C11,但 C++ 标准将始终明确这一点。

If I'm looking at the C standard with reference to C++ how could I possibly know whether the area I'm looking at has been overridden?

您查看 C++ 标准,它要么显式导入 C 定义减去 restrict 或它想要的任何 C 行为,要么进行显式修改。

通常,阅读好的文档而不是标准本身就可以了。


解决您最初的问题:

The author is citing from the C99 standard in C++ documentation. Is that valid?

是的,因为

1 Tables 74 [contains std::tolower, me], 75, 76, 77, 78, and 79 describe headers <cctype>, <cwctype>, <cstring>, <cwchar>, <cstdlib> (character conversions), and <cuchar>, respectively.
2 The contents of these headers shall be the same as the Standard C Library headers <ctype.h>, <wctype.h>, <string.h>, <wchar.h>, and <stdlib.h> and the C Unicode TR header <uchar.h>, respectively, with the following modifications [none of those apply to std::tolower, me]:

21.8 [c.strings] 在 N4140

编辑正确,此特定文本自 C90 以来一直在标准中。

来自 C90 4.3

The header declares several functions useful for testing and mapping characters. In all cases the argument is an int , the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF . If the argument has any other value, the behavior is undefined.

从 C11 7.4/1

The header declares several functions useful for classifying and mapping characters. In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.

相同的文字; C一直都是这样。因此,您的特定 C++ 版本使用哪个 C 版本并不重要,因为所有 C 版本都是等效的。