memcpy(dest, src, 0) 是在 C++ 标准中定义的吗?
Is memcpy(dest, src, 0) defined in the C++ standard?
This question and 是该站点上的众多问题之一,这些问题询问对 memcpy()
的调用是否有效且 length\size 指定为零值。
回答的时候大家引用当前的C标准(这里是C17 ISO/IEC9899:2017 page 283),
Where an argument declared as size_t
n
specifies the length of the array for a function, n
can have
the value zero on a call to that function. Unless explicitly stated otherwise in the description of a
particular function in this subclause, pointer arguments on such a call shall still have valid values, as
described in 7.1.4. On such a call, a function that locates a character finds no occurrence, a function
that compares two character sequences returns zero, and a function that copies characters copies
zero characters.
然而,这是来自C标准,不是引用自C++标准。
在当前的 C++ 标准(即 C++17 ISO/IEC 14882)中,是否列出了相同的定义? C 和 C++ 有两种不同的标准(和语言),根据我的理解,你不能引用一个并期望相同的 rule/behavior出现在其他标准中。
如果 C 标准中的这段引用在 C++ 中有效,但没有在标准中明确说明,那么有人可以提供来源支持 C 和 C++?
之间的连接
C++17 标准是这样说的 the C standard library:
1
The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.
2
The descriptions of many library functions rely on the C standard library for the semantics of those functions. In some cases, the signatures specified in this International Standard may be different from the signatures in the C standard library, and additional overloads may be declared in this International Standard, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict
qualifier) are the same unless otherwise stated.
关于你的问题,
If this quotation from the C standard is valid in C++ without explicitly stating it in the standard
答案是"yes"。
[library.c]/2,强调我的:
The descriptions of many library functions rely on the C standard library for the semantics of those functions.
In some cases, the signatures specified in this document may be different from the signatures in the C standard library, and additional overloads may be declared in this document, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.
The contents and meaning of the header <cstring>
are the same as the C standard library header <string.h>
.
C 库有点融入了 C++。正如 C++17 标准(强调我的)的 [intro.scope] 中所说:
- C++ is a general purpose programming language based on the C programming language as described in
ISO/IEC 9899:2011 Programming languages — C (hereinafter referred to as the C standard). In addition to
the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces,
operator overloading, function name overloading, references, free store management operators, and additional
library facilities.
再一次,在 [intro.refs] 中:
- The library described in Clause 7 of ISO/IEC 9899:2011 is hereinafter called the C standard library.1
1) With the qualifications noted in Clauses 21 through 33 and in C.5, the C standard library is a subset of the C++ standard
library.
所以 C 标准库中的所有内容都在 C++ 中。
This question and memcpy()
的调用是否有效且 length\size 指定为零值。
回答的时候大家引用当前的C标准(这里是C17 ISO/IEC9899:2017 page 283),
Where an argument declared as
size_t
n
specifies the length of the array for a function,n
can have the value zero on a call to that function. Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4. On such a call, a function that locates a character finds no occurrence, a function that compares two character sequences returns zero, and a function that copies characters copies zero characters.
然而,这是来自C标准,不是引用自C++标准。
在当前的 C++ 标准(即 C++17 ISO/IEC 14882)中,是否列出了相同的定义? C 和 C++ 有两种不同的标准(和语言),根据我的理解,你不能引用一个并期望相同的 rule/behavior出现在其他标准中。
如果 C 标准中的这段引用在 C++ 中有效,但没有在标准中明确说明,那么有人可以提供来源支持 C 和 C++?
之间的连接C++17 标准是这样说的 the C standard library:
1 The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.
2 The descriptions of many library functions rely on the C standard library for the semantics of those functions. In some cases, the signatures specified in this International Standard may be different from the signatures in the C standard library, and additional overloads may be declared in this International Standard, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C
restrict
qualifier) are the same unless otherwise stated.
关于你的问题,
If this quotation from the C standard is valid in C++ without explicitly stating it in the standard
答案是"yes"。
[library.c]/2,强调我的:
The descriptions of many library functions rely on the C standard library for the semantics of those functions. In some cases, the signatures specified in this document may be different from the signatures in the C standard library, and additional overloads may be declared in this document, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.
The contents and meaning of the header
<cstring>
are the same as the C standard library header<string.h>
.
C 库有点融入了 C++。正如 C++17 标准(强调我的)的 [intro.scope] 中所说:
- C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:2011 Programming languages — C (hereinafter referred to as the C standard). In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store management operators, and additional library facilities.
再一次,在 [intro.refs] 中:
- The library described in Clause 7 of ISO/IEC 9899:2011 is hereinafter called the C standard library.1
1) With the qualifications noted in Clauses 21 through 33 and in C.5, the C standard library is a subset of the C++ standard library.
所以 C 标准库中的所有内容都在 C++ 中。