在Qt 4.8中
当我#include <cstring> ,我发现我可以直接使用memcpy 。
为什么我不需要使用std::memcpy呢?
为什么编译器不报告错误?
In Qt 4.8
When I #include <cstring>, I found that I can use memcpy directly.
Why should I not need to use std::memcpy instead?
Why the complier does not report an error?
最满意答案
标准说(§17.5.1.2/ 4)
除第17至30条和附录D中所述外,每个标题cname的内容与C标准库(1.2)中指定的相应标题name.h的内容相同。 但是,在C ++标准库中,声明(除了在C中定义为宏的名称除外)都在命名空间std的命名空间范围(3.3.6)内。 未指定这些名称(包括第18至30条和附件D中添加的任何重载)是否首先在全局命名空间范围内声明,然后通过显式使用声明(7.3.3)注入命名空间std。
因此,当您包含<cstring> ,名称在名称空间std::可用,但允许名称也在全局名称空间中。
同样,当您包含<string.h>该名称保证在全局名称空间中,但也可能在名称空间std可见。
The standard says (§17.5.1.2/4)
Except as noted in Clauses 17 through 30 and Annex D, the contents of each header cname is the same as that of the corresponding header name.h as specified in the C standard library (1.2). In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names (including any overloads added in Clauses 18 through 30 and Annex D) are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).
So when you include <cstring> the name is available in namespace std::, but it is allowed for the name to also be in the global namespace.
Likewise, when you include <string.h> the name is guaranteed to be in the global namespace, but might also be visible in namespace std.
更多推荐
发布评论