有人可以解释一下以下内容吗:
$ cat test.cpp
#include <string>
std::string div;
$ g++ -c test.cpp
$ g++ -std=c++11 -c test.cpp
test.cpp:2:13: error: 'std::string div' redeclared as different kind of symbol
In file included from /usr/include/c++/4.7.1/cstdlib:66:0,
from /usr/include/c++/4.7.1/ext/string_conversions.h:37,
from /usr/include/c++/4.7.1/bits/basic_string.h:2814,
from /usr/include/c++/4.7.1/string:54,
from test.cpp:1:
/usr/include/stdlib.h:787:14: error: previous declaration of 'div_t div(int, int)'
$
不应该是div
符号在std
命名空间也适用于 C++11 模式?还是我的系统特有的?
/usr/include/stdlib.h
中的每个名字.h
C stdlib 标头驻留在全局命名空间中(显然)。
此外,任何cHEADER
C++ stdlib 标头将定义相应的名称HEADER.h
in the std
命名空间,但也允许将它们放在全局命名空间中(这样它们就可以做
// cHEADER
#include <HEADER.h>
namespace std{
using ::one_name_from_HEADER;
using ::another_name_from_HEADER;
// and so on...
}
并完成它)。
§D.5 [depr.c.headers]
p2 每个 C 标头,每个标头都有一个以下形式的名称name.h
,其行为就像每个名称由相应的名称空间放置在标准库名称空间中一样cname
header 放置在全局命名空间范围内。未指定这些名称是否首先在命名空间 std 的命名空间范围 (3.3.6) 内声明或定义,然后通过显式 using 声明 (7.3.3) 注入全局命名空间范围。
p3 [ Example:标题<cstdlib>
确实在命名空间 std 中提供其声明和定义。它还可以在全局命名空间内提供这些名称。标题<stdlib.h>
确实在全局命名空间中提供了相同的声明和定义,就像在 C 标准中一样。它还可以在命名空间 std 中提供这些名称。—结束示例 ]
正如你所看到的,反之亦然(<HEADER.h>
可能会介绍名字std
命名空间,就好像
// HEADER.h
#include <cHEADER>
using std::one_name_from_HEADER;
using std::another_name_from_HEADER;
// and so on...
}
),这使得这些标头之间的整体区别相当......毫无用处,真的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)